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_CXFA_FMSIMPLEEXPRESSION_H_
8 #define XFA_FXFA_FM2JS_CXFA_FMSIMPLEEXPRESSION_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "xfa/fxfa/fm2js/cxfa_fmlexer.h"
14 
15 enum XFA_FM_AccessorIndex {
16   ACCESSOR_NO_INDEX,
17   ACCESSOR_NO_RELATIVEINDEX,
18   ACCESSOR_POSITIVE_INDEX,
19   ACCESSOR_NEGATIVE_INDEX
20 };
21 
22 enum class ReturnType { kImplied, kInfered };
23 
24 class CFX_WideTextBuf;
25 
26 class CXFA_FMSimpleExpression {
27  public:
28   virtual ~CXFA_FMSimpleExpression() = default;
29   virtual bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) = 0;
30 
31   XFA_FM_TOKEN GetOperatorToken() const;
32 
33  protected:
34   explicit CXFA_FMSimpleExpression(XFA_FM_TOKEN op);
35 
36   const XFA_FM_TOKEN m_op;
37 };
38 
39 class CXFA_FMNullExpression final : public CXFA_FMSimpleExpression {
40  public:
41   CXFA_FMNullExpression();
~CXFA_FMNullExpression()42   ~CXFA_FMNullExpression() override {}
43 
44   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
45 };
46 
47 class CXFA_FMNumberExpression final : public CXFA_FMSimpleExpression {
48  public:
49   explicit CXFA_FMNumberExpression(WideStringView wsNumber);
50   ~CXFA_FMNumberExpression() override;
51 
52   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
53 
54  private:
55   WideStringView m_wsNumber;
56 };
57 
58 class CXFA_FMStringExpression final : public CXFA_FMSimpleExpression {
59  public:
60   explicit CXFA_FMStringExpression(WideStringView wsString);
61   ~CXFA_FMStringExpression() override;
62 
63   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
64 
65  private:
66   WideStringView m_wsString;
67 };
68 
69 class CXFA_FMIdentifierExpression final : public CXFA_FMSimpleExpression {
70  public:
71   explicit CXFA_FMIdentifierExpression(WideStringView wsIdentifier);
72   ~CXFA_FMIdentifierExpression() override;
73 
74   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
75 
76  private:
77   WideStringView m_wsIdentifier;
78 };
79 
80 class CXFA_FMAssignExpression final : public CXFA_FMSimpleExpression {
81  public:
82   CXFA_FMAssignExpression(XFA_FM_TOKEN op,
83                           std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
84                           std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
85   ~CXFA_FMAssignExpression() override;
86 
87   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
88 
89  private:
90   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp1;
91   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp2;
92 };
93 
94 class CXFA_FMBinExpression : public CXFA_FMSimpleExpression {
95  public:
96   ~CXFA_FMBinExpression() override;
97 
98   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
99 
100  protected:
101   CXFA_FMBinExpression(const WideString& opName,
102                        XFA_FM_TOKEN op,
103                        std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
104                        std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
105 
106  private:
107   WideString m_OpName;
108   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp1;
109   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp2;
110 };
111 
112 class CXFA_FMLogicalOrExpression final : public CXFA_FMBinExpression {
113  public:
114   CXFA_FMLogicalOrExpression(XFA_FM_TOKEN op,
115                              std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
116                              std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMLogicalOrExpression()117   ~CXFA_FMLogicalOrExpression() override {}
118 };
119 
120 class CXFA_FMLogicalAndExpression final : public CXFA_FMBinExpression {
121  public:
122   CXFA_FMLogicalAndExpression(XFA_FM_TOKEN op,
123                               std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
124                               std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMLogicalAndExpression()125   ~CXFA_FMLogicalAndExpression() override {}
126 };
127 
128 class CXFA_FMEqualExpression final : public CXFA_FMBinExpression {
129  public:
130   CXFA_FMEqualExpression(XFA_FM_TOKEN op,
131                          std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
132                          std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMEqualExpression()133   ~CXFA_FMEqualExpression() override {}
134 };
135 
136 class CXFA_FMNotEqualExpression final : public CXFA_FMBinExpression {
137  public:
138   CXFA_FMNotEqualExpression(XFA_FM_TOKEN op,
139                             std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
140                             std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMNotEqualExpression()141   ~CXFA_FMNotEqualExpression() override {}
142 };
143 
144 class CXFA_FMGtExpression final : public CXFA_FMBinExpression {
145  public:
146   CXFA_FMGtExpression(XFA_FM_TOKEN op,
147                       std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
148                       std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMGtExpression()149   ~CXFA_FMGtExpression() override {}
150 };
151 
152 class CXFA_FMGeExpression final : public CXFA_FMBinExpression {
153  public:
154   CXFA_FMGeExpression(XFA_FM_TOKEN op,
155                       std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
156                       std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMGeExpression()157   ~CXFA_FMGeExpression() override {}
158 };
159 
160 class CXFA_FMLtExpression final : public CXFA_FMBinExpression {
161  public:
162   CXFA_FMLtExpression(XFA_FM_TOKEN op,
163                       std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
164                       std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMLtExpression()165   ~CXFA_FMLtExpression() override {}
166 };
167 
168 class CXFA_FMLeExpression final : public CXFA_FMBinExpression {
169  public:
170   CXFA_FMLeExpression(XFA_FM_TOKEN op,
171                       std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
172                       std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMLeExpression()173   ~CXFA_FMLeExpression() override {}
174 };
175 
176 class CXFA_FMPlusExpression final : public CXFA_FMBinExpression {
177  public:
178   CXFA_FMPlusExpression(XFA_FM_TOKEN op,
179                         std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
180                         std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMPlusExpression()181   ~CXFA_FMPlusExpression() override {}
182 };
183 
184 class CXFA_FMMinusExpression final : public CXFA_FMBinExpression {
185  public:
186   CXFA_FMMinusExpression(XFA_FM_TOKEN op,
187                          std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
188                          std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMMinusExpression()189   ~CXFA_FMMinusExpression() override {}
190 };
191 
192 class CXFA_FMMulExpression final : public CXFA_FMBinExpression {
193  public:
194   CXFA_FMMulExpression(XFA_FM_TOKEN op,
195                        std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
196                        std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMMulExpression()197   ~CXFA_FMMulExpression() override {}
198 };
199 
200 class CXFA_FMDivExpression final : public CXFA_FMBinExpression {
201  public:
202   CXFA_FMDivExpression(XFA_FM_TOKEN op,
203                        std::unique_ptr<CXFA_FMSimpleExpression> pExp1,
204                        std::unique_ptr<CXFA_FMSimpleExpression> pExp2);
~CXFA_FMDivExpression()205   ~CXFA_FMDivExpression() override {}
206 };
207 
208 class CXFA_FMUnaryExpression : public CXFA_FMSimpleExpression {
209  public:
210   ~CXFA_FMUnaryExpression() override;
211 
212   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
213 
214  protected:
215   CXFA_FMUnaryExpression(const WideString& opName,
216                          XFA_FM_TOKEN op,
217                          std::unique_ptr<CXFA_FMSimpleExpression> pExp);
218 
219  private:
220   WideString m_OpName;
221   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp;
222 };
223 
224 class CXFA_FMPosExpression final : public CXFA_FMUnaryExpression {
225  public:
226   explicit CXFA_FMPosExpression(std::unique_ptr<CXFA_FMSimpleExpression> pExp);
~CXFA_FMPosExpression()227   ~CXFA_FMPosExpression() override {}
228 };
229 
230 class CXFA_FMNegExpression final : public CXFA_FMUnaryExpression {
231  public:
232   explicit CXFA_FMNegExpression(std::unique_ptr<CXFA_FMSimpleExpression> pExp);
~CXFA_FMNegExpression()233   ~CXFA_FMNegExpression() override {}
234 };
235 
236 class CXFA_FMNotExpression final : public CXFA_FMUnaryExpression {
237  public:
238   explicit CXFA_FMNotExpression(std::unique_ptr<CXFA_FMSimpleExpression> pExp);
~CXFA_FMNotExpression()239   ~CXFA_FMNotExpression() override {}
240 };
241 
242 class CXFA_FMCallExpression final : public CXFA_FMSimpleExpression {
243  public:
244   CXFA_FMCallExpression(
245       std::unique_ptr<CXFA_FMSimpleExpression> pExp,
246       std::vector<std::unique_ptr<CXFA_FMSimpleExpression>>&& pArguments,
247       bool bIsSomMethod);
248   ~CXFA_FMCallExpression() override;
249 
250   bool IsBuiltInFunc(CFX_WideTextBuf* funcName);
251   uint32_t IsMethodWithObjParam(const WideString& methodName);
252   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
253 
254  private:
255   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp;
256   bool m_bIsSomMethod;
257   std::vector<std::unique_ptr<CXFA_FMSimpleExpression>> m_Arguments;
258 };
259 
260 class CXFA_FMDotAccessorExpression final : public CXFA_FMSimpleExpression {
261  public:
262   CXFA_FMDotAccessorExpression(
263       std::unique_ptr<CXFA_FMSimpleExpression> pAccessor,
264       XFA_FM_TOKEN op,
265       WideStringView wsIdentifier,
266       std::unique_ptr<CXFA_FMSimpleExpression> pIndexExp);
267   ~CXFA_FMDotAccessorExpression() override;
268 
269   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
270 
271  private:
272   WideStringView m_wsIdentifier;
273   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp1;
274   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp2;
275 };
276 
277 class CXFA_FMIndexExpression final : public CXFA_FMSimpleExpression {
278  public:
279   CXFA_FMIndexExpression(XFA_FM_AccessorIndex accessorIndex,
280                          std::unique_ptr<CXFA_FMSimpleExpression> pIndexExp,
281                          bool bIsStarIndex);
282   ~CXFA_FMIndexExpression() override;
283 
284   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
285 
286  private:
287   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp;
288   XFA_FM_AccessorIndex m_accessorIndex;
289   bool m_bIsStarIndex;
290 };
291 
292 class CXFA_FMDotDotAccessorExpression final : public CXFA_FMSimpleExpression {
293  public:
294   CXFA_FMDotDotAccessorExpression(
295       std::unique_ptr<CXFA_FMSimpleExpression> pAccessor,
296       XFA_FM_TOKEN op,
297       WideStringView wsIdentifier,
298       std::unique_ptr<CXFA_FMSimpleExpression> pIndexExp);
299   ~CXFA_FMDotDotAccessorExpression() override;
300 
301   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
302 
303  private:
304   WideStringView m_wsIdentifier;
305   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp1;
306   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp2;
307 };
308 
309 class CXFA_FMMethodCallExpression final : public CXFA_FMSimpleExpression {
310  public:
311   CXFA_FMMethodCallExpression(
312       std::unique_ptr<CXFA_FMSimpleExpression> pAccessorExp1,
313       std::unique_ptr<CXFA_FMSimpleExpression> pCallExp);
314   ~CXFA_FMMethodCallExpression() override;
315 
316   bool ToJavaScript(CFX_WideTextBuf* js, ReturnType type) override;
317 
318  private:
319   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp1;
320   std::unique_ptr<CXFA_FMSimpleExpression> m_pExp2;
321 };
322 
323 bool CXFA_IsTooBig(const CFX_WideTextBuf* js);
324 
325 #endif  // XFA_FXFA_FM2JS_CXFA_FMSIMPLEEXPRESSION_H_
326