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