1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox HDD container test utility - scripting engine, AST related structures.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #ifndef _VDScriptAst_h__
|
---|
18 | #define _VDScriptAst_h__
|
---|
19 |
|
---|
20 | #include <iprt/list.h>
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Position information.
|
---|
24 | */
|
---|
25 | typedef struct VDSRCPOS
|
---|
26 | {
|
---|
27 | /** Line in the source. */
|
---|
28 | unsigned iLine;
|
---|
29 | /** Current start character .*/
|
---|
30 | unsigned iChStart;
|
---|
31 | /** Current end character. */
|
---|
32 | unsigned iChEnd;
|
---|
33 | } VDSRCPOS;
|
---|
34 | /** Pointer to a source position. */
|
---|
35 | typedef struct VDSRCPOS *PVDSRCPOS;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * AST node classes.
|
---|
39 | */
|
---|
40 | typedef enum VDSCRIPTASTCLASS
|
---|
41 | {
|
---|
42 | /** Invalid. */
|
---|
43 | VDSCRIPTASTCLASS_INVALID = 0,
|
---|
44 | /** Function node. */
|
---|
45 | VDSCRIPTASTCLASS_FUNCTION,
|
---|
46 | /** Function argument. */
|
---|
47 | VDSCRIPTASTCLASS_FUNCTIONARG,
|
---|
48 | /** Identifier node. */
|
---|
49 | VDSCRIPTASTCLASS_IDENTIFIER,
|
---|
50 | /** Declaration node. */
|
---|
51 | VDSCRIPTASTCLASS_DECLARATION,
|
---|
52 | /** Statement node. */
|
---|
53 | VDSCRIPTASTCLASS_STATEMENT,
|
---|
54 | /** Expression node. */
|
---|
55 | VDSCRIPTASTCLASS_EXPRESSION,
|
---|
56 | /** @todo: Add if ... else, loops, ... */
|
---|
57 | /** 32bit blowup. */
|
---|
58 | VDSCRIPTASTCLASS_32BIT_HACK = 0x7fffffff
|
---|
59 | } VDSCRIPTASTCLASS;
|
---|
60 | /** Pointer to an AST node class. */
|
---|
61 | typedef VDSCRIPTASTCLASS *PVDSCRIPTASTCLASS;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Core AST structure.
|
---|
65 | */
|
---|
66 | typedef struct VDSCRIPTASTCORE
|
---|
67 | {
|
---|
68 | /** The node class, used for verification. */
|
---|
69 | VDSCRIPTASTCLASS enmClass;
|
---|
70 | /** List which might be used. */
|
---|
71 | RTLISTNODE ListNode;
|
---|
72 | /** Position in the source file of this node. */
|
---|
73 | VDSRCPOS Pos;
|
---|
74 | } VDSCRIPTASTCORE;
|
---|
75 | /** Pointer to an AST core structure. */
|
---|
76 | typedef VDSCRIPTASTCORE *PVDSCRIPTASTCORE;
|
---|
77 |
|
---|
78 | /** Pointer to an statement node - forward declaration. */
|
---|
79 | typedef struct VDSCRIPTASTSTMT *PVDSCRIPTASTSTMT;
|
---|
80 | /** Pointer to an expression node - forward declaration. */
|
---|
81 | typedef struct VDSCRIPTASTEXPR *PVDSCRIPTASTEXPR;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * AST identifier node.
|
---|
85 | */
|
---|
86 | typedef struct VDSCRIPTASTIDE
|
---|
87 | {
|
---|
88 | /** Core structure. */
|
---|
89 | VDSCRIPTASTCORE Core;
|
---|
90 | /** Number of characters in the identifier, excluding the zero terminator. */
|
---|
91 | unsigned cchIde;
|
---|
92 | /** Identifier, variable size. */
|
---|
93 | char aszIde[1];
|
---|
94 | } VDSCRIPTASTIDE;
|
---|
95 | /** Pointer to an identifer node. */
|
---|
96 | typedef VDSCRIPTASTIDE *PVDSCRIPTASTIDE;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * AST declaration node.
|
---|
100 | */
|
---|
101 | typedef struct VDSCRIPTASTDECL
|
---|
102 | {
|
---|
103 | /** Core structure. */
|
---|
104 | VDSCRIPTASTCORE Core;
|
---|
105 | /** @todo */
|
---|
106 | } VDSCRIPTASTDECL;
|
---|
107 | /** Pointer to an declaration node. */
|
---|
108 | typedef VDSCRIPTASTDECL *PVDSCRIPTASTDECL;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Expression types.
|
---|
112 | */
|
---|
113 | typedef enum VDSCRIPTEXPRTYPE
|
---|
114 | {
|
---|
115 | /** Invalid. */
|
---|
116 | VDSCRIPTEXPRTYPE_INVALID = 0,
|
---|
117 | /** Numerical constant. */
|
---|
118 | VDSCRIPTEXPRTYPE_PRIMARY_NUMCONST,
|
---|
119 | /** String constant. */
|
---|
120 | VDSCRIPTEXPRTYPE_PRIMARY_STRINGCONST,
|
---|
121 | /** Identifier. */
|
---|
122 | VDSCRIPTEXPRTYPE_PRIMARY_IDENTIFIER,
|
---|
123 | /** List of assignment expressions as in a = b = c = ... . */
|
---|
124 | VDSCRIPTEXPRTYPE_ASSIGNMENT_LIST,
|
---|
125 | /** Postfix increment expression. */
|
---|
126 | VDSCRIPTEXPRTYPE_POSTFIX_INCREMENT,
|
---|
127 | /** Postfix decrement expression. */
|
---|
128 | VDSCRIPTEXPRTYPE_POSTFIX_DECREMENT,
|
---|
129 | /** Postfix function call expression. */
|
---|
130 | VDSCRIPTEXPRTYPE_POSTFIX_FNCALL,
|
---|
131 | /** Unary increment expression. */
|
---|
132 | VDSCRIPTEXPRTYPE_UNARY_INCREMENT,
|
---|
133 | /** Unary decrement expression. */
|
---|
134 | VDSCRIPTEXPRTYPE_UNARY_DECREMENT,
|
---|
135 | /** Unary positive sign expression. */
|
---|
136 | VDSCRIPTEXPRTYPE_UNARY_POSSIGN,
|
---|
137 | /** Unary negtive sign expression. */
|
---|
138 | VDSCRIPTEXPRTYPE_UNARY_NEGSIGN,
|
---|
139 | /** Unary invert expression. */
|
---|
140 | VDSCRIPTEXPRTYPE_UNARY_INVERT,
|
---|
141 | /** Unary negate expression. */
|
---|
142 | VDSCRIPTEXPRTYPE_UNARY_NEGATE,
|
---|
143 | /** Multiplicative expression. */
|
---|
144 | VDSCRIPTEXPRTYPE_MULTIPLICATION,
|
---|
145 | /** Division expression. */
|
---|
146 | VDSCRIPTEXPRTYPE_DIVISION,
|
---|
147 | /** Modulus expression. */
|
---|
148 | VDSCRIPTEXPRTYPE_MODULUS,
|
---|
149 | /** Addition expression. */
|
---|
150 | VDSCRIPTEXPRTYPE_ADDITION,
|
---|
151 | /** Subtraction expression. */
|
---|
152 | VDSCRIPTEXPRTYPE_SUBTRACTION,
|
---|
153 | /** Logical shift right. */
|
---|
154 | VDSCRIPTEXPRTYPE_LSR,
|
---|
155 | /** Logical shift left. */
|
---|
156 | VDSCRIPTEXPRTYPE_LSL,
|
---|
157 | /** Lower than expression */
|
---|
158 | VDSCRIPTEXPRTYPE_LOWER,
|
---|
159 | /** Higher than expression */
|
---|
160 | VDSCRIPTEXPRTYPE_HIGHER,
|
---|
161 | /** Lower or equal than expression */
|
---|
162 | VDSCRIPTEXPRTYPE_LOWEREQUAL,
|
---|
163 | /** Higher or equal than expression */
|
---|
164 | VDSCRIPTEXPRTYPE_HIGHEREQUAL,
|
---|
165 | /** Equals expression */
|
---|
166 | VDSCRIPTEXPRTYPE_EQUAL,
|
---|
167 | /** Not equal expression */
|
---|
168 | VDSCRIPTEXPRTYPE_NOTEQUAL,
|
---|
169 | /** Bitwise and expression */
|
---|
170 | VDSCRIPTEXPRTYPE_BITWISE_AND,
|
---|
171 | /** Bitwise xor expression */
|
---|
172 | VDSCRIPTEXPRTYPE_BITWISE_XOR,
|
---|
173 | /** Bitwise or expression */
|
---|
174 | VDSCRIPTEXPRTYPE_BITWISE_OR,
|
---|
175 | /** Logical and expression */
|
---|
176 | VDSCRIPTEXPRTYPE_LOGICAL_AND,
|
---|
177 | /** Logical or expression */
|
---|
178 | VDSCRIPTEXPRTYPE_LOGICAL_OR,
|
---|
179 | /** Assign expression */
|
---|
180 | VDSCRIPTEXPRTYPE_ASSIGN,
|
---|
181 | /** Multiplicative assign expression */
|
---|
182 | VDSCRIPTEXPRTYPE_ASSIGN_MULT,
|
---|
183 | /** Division assign expression */
|
---|
184 | VDSCRIPTEXPRTYPE_ASSIGN_DIV,
|
---|
185 | /** Modulus assign expression */
|
---|
186 | VDSCRIPTEXPRTYPE_ASSIGN_MOD,
|
---|
187 | /** Additive assign expression */
|
---|
188 | VDSCRIPTEXPRTYPE_ASSIGN_ADD,
|
---|
189 | /** Subtractive assign expression */
|
---|
190 | VDSCRIPTEXPRTYPE_ASSIGN_SUB,
|
---|
191 | /** Bitwise left shift assign expression */
|
---|
192 | VDSCRIPTEXPRTYPE_ASSIGN_LSL,
|
---|
193 | /** Bitwise right shift assign expression */
|
---|
194 | VDSCRIPTEXPRTYPE_ASSIGN_LSR,
|
---|
195 | /** Bitwise and assign expression */
|
---|
196 | VDSCRIPTEXPRTYPE_ASSIGN_AND,
|
---|
197 | /** Bitwise xor assign expression */
|
---|
198 | VDSCRIPTEXPRTYPE_ASSIGN_XOR,
|
---|
199 | /** Bitwise or assign expression */
|
---|
200 | VDSCRIPTEXPRTYPE_ASSIGN_OR,
|
---|
201 | /** 32bit hack. */
|
---|
202 | VDSCRIPTEXPRTYPE_32BIT_HACK = 0x7fffffff
|
---|
203 | } VDSCRIPTEXPRTYPE;
|
---|
204 | /** Pointer to an expression type. */
|
---|
205 | typedef VDSCRIPTEXPRTYPE *PVDSCRIPTEXPRTYPE;
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * AST expression node.
|
---|
209 | */
|
---|
210 | typedef struct VDSCRIPTASTEXPR
|
---|
211 | {
|
---|
212 | /** Core structure. */
|
---|
213 | VDSCRIPTASTCORE Core;
|
---|
214 | /** Expression type. */
|
---|
215 | VDSCRIPTEXPRTYPE enmType;
|
---|
216 | /** Expression type dependent data. */
|
---|
217 | union
|
---|
218 | {
|
---|
219 | /** Numerical constant. */
|
---|
220 | uint64_t u64;
|
---|
221 | /** Primary identifier. */
|
---|
222 | PVDSCRIPTASTIDE pIde;
|
---|
223 | /** String literal */
|
---|
224 | const char *pszStr;
|
---|
225 | /** List of expressions - VDSCRIPTASTEXPR. */
|
---|
226 | RTLISTANCHOR ListExpr;
|
---|
227 | /** Pointer to another expression. */
|
---|
228 | PVDSCRIPTASTEXPR pExpr;
|
---|
229 | /** Function call expression. */
|
---|
230 | struct
|
---|
231 | {
|
---|
232 | /** Other postfix expression used as the identifier for the function. */
|
---|
233 | PVDSCRIPTASTEXPR pFnIde;
|
---|
234 | /** Argument list if existing. */
|
---|
235 | RTLISTANCHOR ListArgs;
|
---|
236 | } FnCall;
|
---|
237 | /** Binary operation. */
|
---|
238 | struct
|
---|
239 | {
|
---|
240 | /** Left operator. */
|
---|
241 | PVDSCRIPTASTEXPR pLeftExpr;
|
---|
242 | /** Right operator. */
|
---|
243 | PVDSCRIPTASTEXPR pRightExpr;
|
---|
244 | } BinaryOp;
|
---|
245 | };
|
---|
246 | } VDSCRIPTASTEXPR;
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * AST if node.
|
---|
250 | */
|
---|
251 | typedef struct VDSCRIPTASTIF
|
---|
252 | {
|
---|
253 | /** Conditional expression. */
|
---|
254 | PVDSCRIPTASTEXPR pCond;
|
---|
255 | /** The true branch */
|
---|
256 | PVDSCRIPTASTSTMT pTrueStmt;
|
---|
257 | /** The else branch, can be NULL if no else branch. */
|
---|
258 | PVDSCRIPTASTSTMT pElseStmt;
|
---|
259 | } VDSCRIPTASTIF;
|
---|
260 | /** Pointer to an expression node. */
|
---|
261 | typedef VDSCRIPTASTIF *PVDSCRIPTASTIF;
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * AST switch node.
|
---|
265 | */
|
---|
266 | typedef struct VDSCRIPTASTSWITCH
|
---|
267 | {
|
---|
268 | /** Conditional expression. */
|
---|
269 | PVDSCRIPTASTEXPR pCond;
|
---|
270 | /** The statement to follow. */
|
---|
271 | PVDSCRIPTASTSTMT pStmt;
|
---|
272 | } VDSCRIPTASTSWITCH;
|
---|
273 | /** Pointer to an expression node. */
|
---|
274 | typedef VDSCRIPTASTSWITCH *PVDSCRIPTASTSWITCH;
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * AST while or do ... while node.
|
---|
278 | */
|
---|
279 | typedef struct VDSCRIPTASTWHILE
|
---|
280 | {
|
---|
281 | /** Flag whether this is a do while loop. */
|
---|
282 | bool fDoWhile;
|
---|
283 | /** Conditional expression. */
|
---|
284 | PVDSCRIPTASTEXPR pCond;
|
---|
285 | /** The statement to follow. */
|
---|
286 | PVDSCRIPTASTSTMT pStmt;
|
---|
287 | } VDSCRIPTASTWHILE;
|
---|
288 | /** Pointer to an expression node. */
|
---|
289 | typedef VDSCRIPTASTWHILE *PVDSCRIPTASTWHILE;
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * AST for node.
|
---|
293 | */
|
---|
294 | typedef struct VDSCRIPTASTFOR
|
---|
295 | {
|
---|
296 | /** Initializer expression. */
|
---|
297 | PVDSCRIPTASTEXPR pExprStart;
|
---|
298 | /** The exit condition. */
|
---|
299 | PVDSCRIPTASTEXPR pExprCond;
|
---|
300 | /** The third expression (normally used to increase/decrease loop variable). */
|
---|
301 | PVDSCRIPTASTEXPR pExpr3;
|
---|
302 | /** The for loop body. */
|
---|
303 | PVDSCRIPTASTSTMT pStmt;
|
---|
304 | } VDSCRIPTASTFOR;
|
---|
305 | /** Pointer to an expression node. */
|
---|
306 | typedef VDSCRIPTASTFOR *PVDSCRIPTASTFOR;
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Statement types.
|
---|
310 | */
|
---|
311 | typedef enum VDSCRIPTSTMTTYPE
|
---|
312 | {
|
---|
313 | /** Invalid. */
|
---|
314 | VDSCRIPTSTMTTYPE_INVALID = 0,
|
---|
315 | /** Compound statement. */
|
---|
316 | VDSCRIPTSTMTTYPE_COMPOUND,
|
---|
317 | /** Expression statement. */
|
---|
318 | VDSCRIPTSTMTTYPE_EXPRESSION,
|
---|
319 | /** if statement. */
|
---|
320 | VDSCRIPTSTMTTYPE_IF,
|
---|
321 | /** switch statement. */
|
---|
322 | VDSCRIPTSTMTTYPE_SWITCH,
|
---|
323 | /** while statement. */
|
---|
324 | VDSCRIPTSTMTTYPE_WHILE,
|
---|
325 | /** for statement. */
|
---|
326 | VDSCRIPTSTMTTYPE_FOR,
|
---|
327 | /** continue statement. */
|
---|
328 | VDSCRIPTSTMTTYPE_CONTINUE,
|
---|
329 | /** break statement. */
|
---|
330 | VDSCRIPTSTMTTYPE_BREAK,
|
---|
331 | /** return statement. */
|
---|
332 | VDSCRIPTSTMTTYPE_RETURN,
|
---|
333 | /** case statement. */
|
---|
334 | VDSCRIPTSTMTTYPE_CASE,
|
---|
335 | /** default statement. */
|
---|
336 | VDSCRIPTSTMTTYPE_DEFAULT,
|
---|
337 | /** 32bit hack. */
|
---|
338 | VDSCRIPTSTMTTYPE_32BIT_HACK = 0x7fffffff
|
---|
339 | } VDSCRIPTSTMTTYPE;
|
---|
340 | /** Pointer to a statement type. */
|
---|
341 | typedef VDSCRIPTSTMTTYPE *PVDSCRIPTSTMTTYPE;
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * AST statement node.
|
---|
345 | */
|
---|
346 | typedef struct VDSCRIPTASTSTMT
|
---|
347 | {
|
---|
348 | /** Core structure. */
|
---|
349 | VDSCRIPTASTCORE Core;
|
---|
350 | /** Statement type */
|
---|
351 | VDSCRIPTSTMTTYPE enmStmtType;
|
---|
352 | /** Statement type dependent data. */
|
---|
353 | union
|
---|
354 | {
|
---|
355 | /** Compound statement. */
|
---|
356 | struct
|
---|
357 | {
|
---|
358 | /** List of declarations - VDSCRIPTASTDECL. */
|
---|
359 | RTLISTANCHOR ListDecls;
|
---|
360 | /** List of statements - VDSCRIPTASTSTMT. */
|
---|
361 | RTLISTANCHOR ListStmts;
|
---|
362 | } Compound;
|
---|
363 | /** case, default statement. */
|
---|
364 | struct
|
---|
365 | {
|
---|
366 | /** Pointer to the expression. */
|
---|
367 | PVDSCRIPTASTEXPR pExpr;
|
---|
368 | /** Pointer to the statement. */
|
---|
369 | PVDSCRIPTASTSTMT pStmt;
|
---|
370 | } Case;
|
---|
371 | /** "if" statement. */
|
---|
372 | VDSCRIPTASTIF If;
|
---|
373 | /** "switch" statement. */
|
---|
374 | VDSCRIPTASTSWITCH Switch;
|
---|
375 | /** "while" or "do ... while" loop. */
|
---|
376 | VDSCRIPTASTWHILE While;
|
---|
377 | /** "for" loop. */
|
---|
378 | VDSCRIPTASTFOR For;
|
---|
379 | /** Pointer to another statement. */
|
---|
380 | PVDSCRIPTASTSTMT pStmt;
|
---|
381 | /** Expression statement. */
|
---|
382 | PVDSCRIPTASTEXPR pExpr;
|
---|
383 | };
|
---|
384 | } VDSCRIPTASTSTMT;
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * AST node for one function argument.
|
---|
388 | */
|
---|
389 | typedef struct VDSCRIPTASTFNARG
|
---|
390 | {
|
---|
391 | /** Core structure. */
|
---|
392 | VDSCRIPTASTCORE Core;
|
---|
393 | /** Identifier describing the type of the argument. */
|
---|
394 | PVDSCRIPTASTIDE pType;
|
---|
395 | /** The name of the argument. */
|
---|
396 | PVDSCRIPTASTIDE pArgIde;
|
---|
397 | } VDSCRIPTASTFNARG;
|
---|
398 | /** Pointer to a AST function argument node. */
|
---|
399 | typedef VDSCRIPTASTFNARG *PVDSCRIPTASTFNARG;
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * AST node describing a function.
|
---|
403 | */
|
---|
404 | typedef struct VDSCRIPTASTFN
|
---|
405 | {
|
---|
406 | /** Core structure. */
|
---|
407 | VDSCRIPTASTCORE Core;
|
---|
408 | /** Identifier describing the return type. */
|
---|
409 | PVDSCRIPTASTIDE pRetType;
|
---|
410 | /** Name of the function. */
|
---|
411 | PVDSCRIPTASTIDE pFnIde;
|
---|
412 | /** Number of arguments in the list. */
|
---|
413 | unsigned cArgs;
|
---|
414 | /** Argument list - VDSCRIPTASTFNARG. */
|
---|
415 | RTLISTANCHOR ListArgs;
|
---|
416 | /** Compound statement node. */
|
---|
417 | PVDSCRIPTASTSTMT pCompoundStmts;
|
---|
418 | } VDSCRIPTASTFN;
|
---|
419 | /** Pointer to a function AST node. */
|
---|
420 | typedef VDSCRIPTASTFN *PVDSCRIPTASTFN;
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * Free the given AST node and all subsequent nodes pointed to
|
---|
424 | * by the given node.
|
---|
425 | *
|
---|
426 | * @returns nothing.
|
---|
427 | * @param pAstNode The AST node to free.
|
---|
428 | */
|
---|
429 | DECLHIDDEN(void) vdScriptAstNodeFree(PVDSCRIPTASTCORE pAstNode);
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Allocate a non variable in size AST node of the given class.
|
---|
433 | *
|
---|
434 | * @returns Pointer to the allocated node.
|
---|
435 | * NULL if out of memory.
|
---|
436 | * @param enmClass The class of the AST node.
|
---|
437 | */
|
---|
438 | DECLHIDDEN(PVDSCRIPTASTCORE) vdScriptAstNodeAlloc(VDSCRIPTASTCLASS enmClass);
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Allocate a IDE node which can hold the given number of characters.
|
---|
442 | *
|
---|
443 | * @returns Pointer to the allocated node.
|
---|
444 | * NULL if out of memory.
|
---|
445 | * @param cchIde Number of characters which can be stored in the node.
|
---|
446 | */
|
---|
447 | DECLHIDDEN(PVDSCRIPTASTIDE) vdScriptAstNodeIdeAlloc(unsigned cchIde);
|
---|
448 |
|
---|
449 | #endif /* _VDScriptAst_h__ */
|
---|