1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
/** @file
* VBox HDD container test utility - scripting engine, AST related structures.
*/
/*
* Copyright (C) 2013-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#ifndef VBOX_INCLUDED_SRC_testcase_VDScriptAst_h
#define VBOX_INCLUDED_SRC_testcase_VDScriptAst_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/list.h>
/**
* Position information.
*/
typedef struct VDSRCPOS
{
/** Line in the source. */
unsigned iLine;
/** Current start character .*/
unsigned iChStart;
/** Current end character. */
unsigned iChEnd;
} VDSRCPOS;
/** Pointer to a source position. */
typedef struct VDSRCPOS *PVDSRCPOS;
/**
* AST node classes.
*/
typedef enum VDSCRIPTASTCLASS
{
/** Invalid. */
VDSCRIPTASTCLASS_INVALID = 0,
/** Function node. */
VDSCRIPTASTCLASS_FUNCTION,
/** Function argument. */
VDSCRIPTASTCLASS_FUNCTIONARG,
/** Identifier node. */
VDSCRIPTASTCLASS_IDENTIFIER,
/** Declaration node. */
VDSCRIPTASTCLASS_DECLARATION,
/** Statement node. */
VDSCRIPTASTCLASS_STATEMENT,
/** Expression node. */
VDSCRIPTASTCLASS_EXPRESSION,
/** Type name node. */
VDSCRIPTASTCLASS_TYPENAME,
/** Type specifier node. */
VDSCRIPTASTCLASS_TYPESPECIFIER,
/** 32bit blowup. */
VDSCRIPTASTCLASS_32BIT_HACK = 0x7fffffff
} VDSCRIPTASTCLASS;
/** Pointer to an AST node class. */
typedef VDSCRIPTASTCLASS *PVDSCRIPTASTCLASS;
/**
* Core AST structure.
*/
typedef struct VDSCRIPTASTCORE
{
/** The node class, used for verification. */
VDSCRIPTASTCLASS enmClass;
/** List which might be used. */
RTLISTNODE ListNode;
/** Position in the source file of this node. */
VDSRCPOS Pos;
} VDSCRIPTASTCORE;
/** Pointer to an AST core structure. */
typedef VDSCRIPTASTCORE *PVDSCRIPTASTCORE;
/** Pointer to an statement node - forward declaration. */
typedef struct VDSCRIPTASTSTMT *PVDSCRIPTASTSTMT;
/** Pointer to an expression node - forward declaration. */
typedef struct VDSCRIPTASTEXPR *PVDSCRIPTASTEXPR;
/**
* AST identifier node.
*/
typedef struct VDSCRIPTASTIDE
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** Number of characters in the identifier, excluding the zero terminator. */
unsigned cchIde;
/** Identifier, variable size. */
char aszIde[1];
} VDSCRIPTASTIDE;
/** Pointer to an identifer node. */
typedef VDSCRIPTASTIDE *PVDSCRIPTASTIDE;
/**
* Type specifier.
*/
typedef enum VDSCRIPTASTTYPESPECIFIER
{
/** Invalid type specifier. */
VDSCRIPTASTTYPESPECIFIER_INVALID = 0,
/** Union type specifier. */
VDSCRIPTASTTYPESPECIFIER_UNION,
/** Struct type specifier. */
VDSCRIPTASTTYPESPECIFIER_STRUCT,
/** Identifier of a typedefed type. */
VDSCRIPTASTTYPESPECIFIER_IDE,
/** 32bit hack. */
VDSCRIPTASTTYPESPECIFIER_32BIT_HACK = 0x7fffffff
} VDSCRIPTASTTYPESPECIFIER;
/** Pointer to a typespecifier. */
typedef VDSCRIPTASTTYPESPECIFIER *PVDSCRIPTASTTYPESPECIFIER;
/**
* AST type specifier.
*/
typedef struct VDSCRIPTASTTYPESPEC
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** Specifier type. */
VDSCRIPTASTTYPESPECIFIER enmType;
/** Type dependent data .*/
union
{
/** Pointer to an identifier for typedefed types. */
PVDSCRIPTASTIDE pIde;
/** struct or union specifier. */
struct
{
/** Pointer to the identifier, optional. */
PVDSCRIPTASTIDE pIde;
/** Declaration list - VDSCRIPTAST. */
RTLISTANCHOR ListDecl;
} StructUnion;
};
} VDSCRIPTASTTYPESPEC;
/** Pointer to an AST type specifier. */
typedef VDSCRIPTASTTYPESPEC *PVDSCRIPTASTTYPESPEC;
/**
* Storage clase specifier.
*/
typedef enum VDSCRIPTASTSTORAGECLASS
{
/** Invalid storage class sepcifier. */
VDSCRIPTASTSTORAGECLASS_INVALID = 0,
/** A typedef type. */
VDSCRIPTASTSTORAGECLASS_TYPEDEF,
/** An external declared object. */
VDSCRIPTASTSTORAGECLASS_EXTERN,
/** A static declared object. */
VDSCRIPTASTSTORAGECLASS_STATIC,
/** Auto object. */
VDSCRIPTASTSTORAGECLASS_AUTO,
/** Object should be stored in a register. */
VDSCRIPTASTSTORAGECLASS_REGISTER,
/** 32bit hack. */
VDSCRIPTASTSTORAGECLASS_32BIT_HACK = 0x7fffffff
} VDSCRIPTASTSTORAGECLASS;
/** Pointer to a storage class. */
typedef VDSCRIPTASTSTORAGECLASS *PVDSCRIPTASTSTORAGECLASS;
/**
* Type qualifier.
*/
typedef enum VDSCRIPTASTTYPEQUALIFIER
{
/** Invalid type qualifier. */
VDSCRIPTASTTYPEQUALIFIER_INVALID = 0,
/** Const type qualifier. */
VDSCRIPTASTTYPEQUALIFIER_CONST,
/** Restrict type qualifier. */
VDSCRIPTASTTYPEQUALIFIER_RESTRICT,
/** Volatile type qualifier. */
VDSCRIPTASTTYPEQUALIFIER_VOLATILE,
/** 32bit hack. */
VDSCRIPTASTTYPEQUALIFIER_32BIT_HACK = 0x7fffffff
} VDSCRIPTASTTYPEQUALIFIER;
/** Pointer to a type qualifier. */
typedef VDSCRIPTASTTYPEQUALIFIER *PVDSCRIPTASTTYPEQUALIFIER;
/**
* AST type name node.
*/
typedef struct VDSCRIPTASTTYPENAME
{
/** Core structure. */
VDSCRIPTASTCORE Core;
} VDSCRIPTASTTYPENAME;
/** Pointer to a type name node. */
typedef VDSCRIPTASTTYPENAME *PVDSCRIPTASTTYPENAME;
/**
* AST declaration node.
*/
typedef struct VDSCRIPTASTDECL
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** @todo */
} VDSCRIPTASTDECL;
/** Pointer to an declaration node. */
typedef VDSCRIPTASTDECL *PVDSCRIPTASTDECL;
/**
* Expression types.
*/
typedef enum VDSCRIPTEXPRTYPE
{
/** Invalid. */
VDSCRIPTEXPRTYPE_INVALID = 0,
/** Numerical constant. */
VDSCRIPTEXPRTYPE_PRIMARY_NUMCONST,
/** String constant. */
VDSCRIPTEXPRTYPE_PRIMARY_STRINGCONST,
/** Boolean constant. */
VDSCRIPTEXPRTYPE_PRIMARY_BOOLEAN,
/** Identifier. */
VDSCRIPTEXPRTYPE_PRIMARY_IDENTIFIER,
/** List of assignment expressions as in a = b = c = ... . */
VDSCRIPTEXPRTYPE_ASSIGNMENT_LIST,
/** Postfix increment expression. */
VDSCRIPTEXPRTYPE_POSTFIX_INCREMENT,
/** Postfix decrement expression. */
VDSCRIPTEXPRTYPE_POSTFIX_DECREMENT,
/** Postfix function call expression. */
VDSCRIPTEXPRTYPE_POSTFIX_FNCALL,
/** Postfix dereference expression. */
VDSCRIPTEXPRTYPE_POSTFIX_DEREFERENCE,
/** Dot operator (@todo: Is there a better name for it?). */
VDSCRIPTEXPRTYPE_POSTFIX_DOT,
/** Unary increment expression. */
VDSCRIPTEXPRTYPE_UNARY_INCREMENT,
/** Unary decrement expression. */
VDSCRIPTEXPRTYPE_UNARY_DECREMENT,
/** Unary positive sign expression. */
VDSCRIPTEXPRTYPE_UNARY_POSSIGN,
/** Unary negtive sign expression. */
VDSCRIPTEXPRTYPE_UNARY_NEGSIGN,
/** Unary invert expression. */
VDSCRIPTEXPRTYPE_UNARY_INVERT,
/** Unary negate expression. */
VDSCRIPTEXPRTYPE_UNARY_NEGATE,
/** Unary reference expression. */
VDSCRIPTEXPRTYPE_UNARY_REFERENCE,
/** Unary dereference expression. */
VDSCRIPTEXPRTYPE_UNARY_DEREFERENCE,
/** Cast expression. */
VDSCRIPTEXPRTYPE_CAST,
/** Multiplicative expression. */
VDSCRIPTEXPRTYPE_MULTIPLICATION,
/** Division expression. */
VDSCRIPTEXPRTYPE_DIVISION,
/** Modulus expression. */
VDSCRIPTEXPRTYPE_MODULUS,
/** Addition expression. */
VDSCRIPTEXPRTYPE_ADDITION,
/** Subtraction expression. */
VDSCRIPTEXPRTYPE_SUBTRACTION,
/** Logical shift right. */
VDSCRIPTEXPRTYPE_LSR,
/** Logical shift left. */
VDSCRIPTEXPRTYPE_LSL,
/** Lower than expression */
VDSCRIPTEXPRTYPE_LOWER,
/** Higher than expression */
VDSCRIPTEXPRTYPE_HIGHER,
/** Lower or equal than expression */
VDSCRIPTEXPRTYPE_LOWEREQUAL,
/** Higher or equal than expression */
VDSCRIPTEXPRTYPE_HIGHEREQUAL,
/** Equals expression */
VDSCRIPTEXPRTYPE_EQUAL,
/** Not equal expression */
VDSCRIPTEXPRTYPE_NOTEQUAL,
/** Bitwise and expression */
VDSCRIPTEXPRTYPE_BITWISE_AND,
/** Bitwise xor expression */
VDSCRIPTEXPRTYPE_BITWISE_XOR,
/** Bitwise or expression */
VDSCRIPTEXPRTYPE_BITWISE_OR,
/** Logical and expression */
VDSCRIPTEXPRTYPE_LOGICAL_AND,
/** Logical or expression */
VDSCRIPTEXPRTYPE_LOGICAL_OR,
/** Assign expression */
VDSCRIPTEXPRTYPE_ASSIGN,
/** Multiplicative assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_MULT,
/** Division assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_DIV,
/** Modulus assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_MOD,
/** Additive assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_ADD,
/** Subtractive assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_SUB,
/** Bitwise left shift assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_LSL,
/** Bitwise right shift assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_LSR,
/** Bitwise and assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_AND,
/** Bitwise xor assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_XOR,
/** Bitwise or assign expression */
VDSCRIPTEXPRTYPE_ASSIGN_OR,
/** 32bit hack. */
VDSCRIPTEXPRTYPE_32BIT_HACK = 0x7fffffff
} VDSCRIPTEXPRTYPE;
/** Pointer to an expression type. */
typedef VDSCRIPTEXPRTYPE *PVDSCRIPTEXPRTYPE;
/**
* AST expression node.
*/
typedef struct VDSCRIPTASTEXPR
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** Expression type. */
VDSCRIPTEXPRTYPE enmType;
/** Expression type dependent data. */
union
{
/** Numerical constant. */
uint64_t u64;
/** Primary identifier. */
PVDSCRIPTASTIDE pIde;
/** String literal */
const char *pszStr;
/** Boolean constant. */
bool f;
/** List of expressions - VDSCRIPTASTEXPR. */
RTLISTANCHOR ListExpr;
/** Pointer to another expression. */
PVDSCRIPTASTEXPR pExpr;
/** Function call expression. */
struct
{
/** Other postfix expression used as the identifier for the function. */
PVDSCRIPTASTEXPR pFnIde;
/** Argument list if existing. */
RTLISTANCHOR ListArgs;
} FnCall;
/** Binary operation. */
struct
{
/** Left operator. */
PVDSCRIPTASTEXPR pLeftExpr;
/** Right operator. */
PVDSCRIPTASTEXPR pRightExpr;
} BinaryOp;
/** Dereference or dot operation. */
struct
{
/** The identifier to access. */
PVDSCRIPTASTIDE pIde;
/** Postfix expression coming after this. */
PVDSCRIPTASTEXPR pExpr;
} Deref;
/** Cast expression. */
struct
{
/** Type name. */
PVDSCRIPTASTTYPENAME pTypeName;
/** Following cast expression. */
PVDSCRIPTASTEXPR pExpr;
} Cast;
};
} VDSCRIPTASTEXPR;
/**
* AST if node.
*/
typedef struct VDSCRIPTASTIF
{
/** Conditional expression. */
PVDSCRIPTASTEXPR pCond;
/** The true branch */
PVDSCRIPTASTSTMT pTrueStmt;
/** The else branch, can be NULL if no else branch. */
PVDSCRIPTASTSTMT pElseStmt;
} VDSCRIPTASTIF;
/** Pointer to an expression node. */
typedef VDSCRIPTASTIF *PVDSCRIPTASTIF;
/**
* AST switch node.
*/
typedef struct VDSCRIPTASTSWITCH
{
/** Conditional expression. */
PVDSCRIPTASTEXPR pCond;
/** The statement to follow. */
PVDSCRIPTASTSTMT pStmt;
} VDSCRIPTASTSWITCH;
/** Pointer to an expression node. */
typedef VDSCRIPTASTSWITCH *PVDSCRIPTASTSWITCH;
/**
* AST while or do ... while node.
*/
typedef struct VDSCRIPTASTWHILE
{
/** Flag whether this is a do while loop. */
bool fDoWhile;
/** Conditional expression. */
PVDSCRIPTASTEXPR pCond;
/** The statement to follow. */
PVDSCRIPTASTSTMT pStmt;
} VDSCRIPTASTWHILE;
/** Pointer to an expression node. */
typedef VDSCRIPTASTWHILE *PVDSCRIPTASTWHILE;
/**
* AST for node.
*/
typedef struct VDSCRIPTASTFOR
{
/** Initializer expression. */
PVDSCRIPTASTEXPR pExprStart;
/** The exit condition. */
PVDSCRIPTASTEXPR pExprCond;
/** The third expression (normally used to increase/decrease loop variable). */
PVDSCRIPTASTEXPR pExpr3;
/** The for loop body. */
PVDSCRIPTASTSTMT pStmt;
} VDSCRIPTASTFOR;
/** Pointer to an expression node. */
typedef VDSCRIPTASTFOR *PVDSCRIPTASTFOR;
/**
* Statement types.
*/
typedef enum VDSCRIPTSTMTTYPE
{
/** Invalid. */
VDSCRIPTSTMTTYPE_INVALID = 0,
/** Compound statement. */
VDSCRIPTSTMTTYPE_COMPOUND,
/** Expression statement. */
VDSCRIPTSTMTTYPE_EXPRESSION,
/** if statement. */
VDSCRIPTSTMTTYPE_IF,
/** switch statement. */
VDSCRIPTSTMTTYPE_SWITCH,
/** while statement. */
VDSCRIPTSTMTTYPE_WHILE,
/** for statement. */
VDSCRIPTSTMTTYPE_FOR,
/** continue statement. */
VDSCRIPTSTMTTYPE_CONTINUE,
/** break statement. */
VDSCRIPTSTMTTYPE_BREAK,
/** return statement. */
VDSCRIPTSTMTTYPE_RETURN,
/** case statement. */
VDSCRIPTSTMTTYPE_CASE,
/** default statement. */
VDSCRIPTSTMTTYPE_DEFAULT,
/** 32bit hack. */
VDSCRIPTSTMTTYPE_32BIT_HACK = 0x7fffffff
} VDSCRIPTSTMTTYPE;
/** Pointer to a statement type. */
typedef VDSCRIPTSTMTTYPE *PVDSCRIPTSTMTTYPE;
/**
* AST statement node.
*/
typedef struct VDSCRIPTASTSTMT
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** Statement type */
VDSCRIPTSTMTTYPE enmStmtType;
/** Statement type dependent data. */
union
{
/** Compound statement. */
struct
{
/** List of declarations - VDSCRIPTASTDECL. */
RTLISTANCHOR ListDecls;
/** List of statements - VDSCRIPTASTSTMT. */
RTLISTANCHOR ListStmts;
} Compound;
/** case, default statement. */
struct
{
/** Pointer to the expression. */
PVDSCRIPTASTEXPR pExpr;
/** Pointer to the statement. */
PVDSCRIPTASTSTMT pStmt;
} Case;
/** "if" statement. */
VDSCRIPTASTIF If;
/** "switch" statement. */
VDSCRIPTASTSWITCH Switch;
/** "while" or "do ... while" loop. */
VDSCRIPTASTWHILE While;
/** "for" loop. */
VDSCRIPTASTFOR For;
/** Pointer to another statement. */
PVDSCRIPTASTSTMT pStmt;
/** Expression statement. */
PVDSCRIPTASTEXPR pExpr;
};
} VDSCRIPTASTSTMT;
/**
* AST node for one function argument.
*/
typedef struct VDSCRIPTASTFNARG
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** Identifier describing the type of the argument. */
PVDSCRIPTASTIDE pType;
/** The name of the argument. */
PVDSCRIPTASTIDE pArgIde;
} VDSCRIPTASTFNARG;
/** Pointer to a AST function argument node. */
typedef VDSCRIPTASTFNARG *PVDSCRIPTASTFNARG;
/**
* AST node describing a function.
*/
typedef struct VDSCRIPTASTFN
{
/** Core structure. */
VDSCRIPTASTCORE Core;
/** Identifier describing the return type. */
PVDSCRIPTASTIDE pRetType;
/** Name of the function. */
PVDSCRIPTASTIDE pFnIde;
/** Number of arguments in the list. */
unsigned cArgs;
/** Argument list - VDSCRIPTASTFNARG. */
RTLISTANCHOR ListArgs;
/** Compound statement node. */
PVDSCRIPTASTSTMT pCompoundStmts;
} VDSCRIPTASTFN;
/** Pointer to a function AST node. */
typedef VDSCRIPTASTFN *PVDSCRIPTASTFN;
/**
* Free the given AST node and all subsequent nodes pointed to
* by the given node.
*
* @returns nothing.
* @param pAstNode The AST node to free.
*/
DECLHIDDEN(void) vdScriptAstNodeFree(PVDSCRIPTASTCORE pAstNode);
/**
* Allocate a non variable in size AST node of the given class.
*
* @returns Pointer to the allocated node.
* NULL if out of memory.
* @param enmClass The class of the AST node.
*/
DECLHIDDEN(PVDSCRIPTASTCORE) vdScriptAstNodeAlloc(VDSCRIPTASTCLASS enmClass);
/**
* Allocate a IDE node which can hold the given number of characters.
*
* @returns Pointer to the allocated node.
* NULL if out of memory.
* @param cchIde Number of characters which can be stored in the node.
*/
DECLHIDDEN(PVDSCRIPTASTIDE) vdScriptAstNodeIdeAlloc(size_t cchIde);
#endif /* !VBOX_INCLUDED_SRC_testcase_VDScriptAst_h */
|