summaryrefslogtreecommitdiffstats
path: root/src/VBox/Storage/testcase/VDScriptAst.cpp
blob: 1880dc3fe33c20f33541f063f40359da84018e4f (plain)
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
/* $Id: VDScriptAst.cpp $ */
/** @file
 * VBox HDD container test utility - scripting engine AST node related functions.
 */

/*
 * Copyright (C) 2013-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */
#define LOGGROUP LOGGROUP_DEFAULT
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/assert.h>
#include <iprt/string.h>

#include <VBox/log.h>

#include "VDScriptAst.h"

/**
 * Put all child nodes of the given expression AST node onto the given to free list.
 *
 * @returns nothing.
 * @param   pList    The free list to append everything to.
 * @param   pAstNode The expression node to free.
 */
static void vdScriptAstNodeExpressionPutOnFreeList(PRTLISTANCHOR pList, PVDSCRIPTASTCORE pAstNode)
{
    AssertMsgReturnVoid(pAstNode->enmClass == VDSCRIPTASTCLASS_EXPRESSION,
                        ("Given AST node is not a statement\n"));

    PVDSCRIPTASTEXPR pExpr = (PVDSCRIPTASTEXPR)pAstNode;
    switch (pExpr->enmType)
    {
        case VDSCRIPTEXPRTYPE_PRIMARY_NUMCONST:
        case VDSCRIPTEXPRTYPE_PRIMARY_BOOLEAN:
            break;
        case VDSCRIPTEXPRTYPE_PRIMARY_STRINGCONST:
            RTStrFree((char *)pExpr->pszStr);
            break;
        case VDSCRIPTEXPRTYPE_PRIMARY_IDENTIFIER:
        {
            RTListAppend(pList, &pExpr->pIde->Core.ListNode);
            break;
        }
        case VDSCRIPTEXPRTYPE_ASSIGNMENT_LIST:
        {
            while (!RTListIsEmpty(&pExpr->ListExpr))
            {
                PVDSCRIPTASTCORE pNode = RTListGetFirst(&pExpr->ListExpr, VDSCRIPTASTCORE, ListNode);
                RTListNodeRemove(&pNode->ListNode);
                RTListAppend(pList, &pNode->ListNode);
            }
            break;
        }
        case VDSCRIPTEXPRTYPE_POSTFIX_FNCALL:
        {
            RTListAppend(pList, &pExpr->FnCall.pFnIde->Core.ListNode);
            while (!RTListIsEmpty(&pExpr->FnCall.ListArgs))
            {
                PVDSCRIPTASTCORE pNode = RTListGetFirst(&pExpr->FnCall.ListArgs, VDSCRIPTASTCORE, ListNode);
                RTListNodeRemove(&pNode->ListNode);
                RTListAppend(pList, &pNode->ListNode);
            }
            break;
        }
        case VDSCRIPTEXPRTYPE_POSTFIX_DEREFERENCE:
        case VDSCRIPTEXPRTYPE_POSTFIX_DOT:
        {
            RTListAppend(pList, &pExpr->Deref.pIde->Core.ListNode);
            RTListAppend(pList, &pExpr->Deref.pExpr->Core.ListNode);
            break;
        }
        case VDSCRIPTEXPRTYPE_POSTFIX_INCREMENT:
        case VDSCRIPTEXPRTYPE_POSTFIX_DECREMENT:
        case VDSCRIPTEXPRTYPE_UNARY_INCREMENT:
        case VDSCRIPTEXPRTYPE_UNARY_DECREMENT:
        case VDSCRIPTEXPRTYPE_UNARY_POSSIGN:
        case VDSCRIPTEXPRTYPE_UNARY_NEGSIGN:
        case VDSCRIPTEXPRTYPE_UNARY_INVERT:
        case VDSCRIPTEXPRTYPE_UNARY_NEGATE:
        case VDSCRIPTEXPRTYPE_UNARY_REFERENCE:
        case VDSCRIPTEXPRTYPE_UNARY_DEREFERENCE:
        {
            RTListAppend(pList, &pExpr->pExpr->Core.ListNode);
            break;
        }
        case VDSCRIPTEXPRTYPE_MULTIPLICATION:
        case VDSCRIPTEXPRTYPE_DIVISION:
        case VDSCRIPTEXPRTYPE_MODULUS:
        case VDSCRIPTEXPRTYPE_ADDITION:
        case VDSCRIPTEXPRTYPE_SUBTRACTION:
        case VDSCRIPTEXPRTYPE_LSR:
        case VDSCRIPTEXPRTYPE_LSL:
        case VDSCRIPTEXPRTYPE_LOWER:
        case VDSCRIPTEXPRTYPE_HIGHER:
        case VDSCRIPTEXPRTYPE_LOWEREQUAL:
        case VDSCRIPTEXPRTYPE_HIGHEREQUAL:
        case VDSCRIPTEXPRTYPE_EQUAL:
        case VDSCRIPTEXPRTYPE_NOTEQUAL:
        case VDSCRIPTEXPRTYPE_BITWISE_AND:
        case VDSCRIPTEXPRTYPE_BITWISE_XOR:
        case VDSCRIPTEXPRTYPE_BITWISE_OR:
        case VDSCRIPTEXPRTYPE_LOGICAL_AND:
        case VDSCRIPTEXPRTYPE_LOGICAL_OR:
        case VDSCRIPTEXPRTYPE_ASSIGN:
        case VDSCRIPTEXPRTYPE_ASSIGN_MULT:
        case VDSCRIPTEXPRTYPE_ASSIGN_DIV:
        case VDSCRIPTEXPRTYPE_ASSIGN_MOD:
        case VDSCRIPTEXPRTYPE_ASSIGN_ADD:
        case VDSCRIPTEXPRTYPE_ASSIGN_SUB:
        case VDSCRIPTEXPRTYPE_ASSIGN_LSL:
        case VDSCRIPTEXPRTYPE_ASSIGN_LSR:
        case VDSCRIPTEXPRTYPE_ASSIGN_AND:
        case VDSCRIPTEXPRTYPE_ASSIGN_XOR:
        case VDSCRIPTEXPRTYPE_ASSIGN_OR:
        {
            RTListAppend(pList, &pExpr->BinaryOp.pLeftExpr->Core.ListNode);
            RTListAppend(pList, &pExpr->BinaryOp.pRightExpr->Core.ListNode);
            break;
        }
        case VDSCRIPTEXPRTYPE_INVALID:
        default:
            AssertMsgFailedReturnVoid(("Invalid AST node expression type %d\n",
                                       pExpr->enmType));
    }
}

/**
 * Free a given statement AST node and put everything on the given to free list.
 *
 * @returns nothing.
 * @param   pList    The free list to append everything to.
 * @param   pAstNode The statement node to free.
 */
static void vdScriptAstNodeStatmentPutOnFreeList(PRTLISTANCHOR pList, PVDSCRIPTASTCORE pAstNode)
{
    AssertMsgReturnVoid(pAstNode->enmClass == VDSCRIPTASTCLASS_STATEMENT,
                        ("Given AST node is not a statement\n"));

    PVDSCRIPTASTSTMT pStmt = (PVDSCRIPTASTSTMT)pAstNode;
    switch (pStmt->enmStmtType)
    {
        case VDSCRIPTSTMTTYPE_COMPOUND:
        {
            /* Put all declarations on the to free list. */
            while (!RTListIsEmpty(&pStmt->Compound.ListDecls))
            {
                PVDSCRIPTASTCORE pNode = RTListGetFirst(&pStmt->Compound.ListDecls, VDSCRIPTASTCORE, ListNode);
                RTListNodeRemove(&pNode->ListNode);
                RTListAppend(pList, &pNode->ListNode);
            }

            /* Put all statements on the to free list. */
            while (!RTListIsEmpty(&pStmt->Compound.ListStmts))
            {
                PVDSCRIPTASTCORE pNode = RTListGetFirst(&pStmt->Compound.ListStmts, VDSCRIPTASTCORE, ListNode);
                RTListNodeRemove(&pNode->ListNode);
                RTListAppend(pList, &pNode->ListNode);
            }
            break;
        }
        case VDSCRIPTSTMTTYPE_EXPRESSION:
        {
            if (pStmt->pExpr)
                RTListAppend(pList, &pStmt->pExpr->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_IF:
        {
            RTListAppend(pList, &pStmt->If.pCond->Core.ListNode);
            RTListAppend(pList, &pStmt->If.pTrueStmt->Core.ListNode);
            if (pStmt->If.pElseStmt)
                RTListAppend(pList, &pStmt->If.pElseStmt->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_SWITCH:
        {
            RTListAppend(pList, &pStmt->Switch.pCond->Core.ListNode);
            RTListAppend(pList, &pStmt->Switch.pStmt->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_WHILE:
        {
            RTListAppend(pList, &pStmt->While.pCond->Core.ListNode);
            RTListAppend(pList, &pStmt->While.pStmt->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_FOR:
        {
            RTListAppend(pList, &pStmt->For.pExprStart->Core.ListNode);
            RTListAppend(pList, &pStmt->For.pExprCond->Core.ListNode);
            RTListAppend(pList, &pStmt->For.pExpr3->Core.ListNode);
            RTListAppend(pList, &pStmt->For.pStmt->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_RETURN:
        {
            if (pStmt->pExpr)
                RTListAppend(pList, &pStmt->pExpr->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_CASE:
        {
            RTListAppend(pList, &pStmt->Case.pExpr->Core.ListNode);
            RTListAppend(pList, &pStmt->Case.pStmt->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_DEFAULT:
        {
            RTListAppend(pList, &pStmt->Case.pStmt->Core.ListNode);
            break;
        }
        case VDSCRIPTSTMTTYPE_CONTINUE:
        case VDSCRIPTSTMTTYPE_BREAK:
            break;
        case VDSCRIPTSTMTTYPE_INVALID:
        default:
            AssertMsgFailedReturnVoid(("Invalid AST node statement type %d\n",
                                       pStmt->enmStmtType));
    }
}

DECLHIDDEN(void) vdScriptAstNodeFree(PVDSCRIPTASTCORE pAstNode)
{
    RTLISTANCHOR ListFree;

    /*
     * The node is not allowed to be part of a list because we need it
     * for the nodes to free list.
     */
    Assert(RTListIsEmpty(&pAstNode->ListNode));
    RTListInit(&ListFree);
    RTListAppend(&ListFree, &pAstNode->ListNode);

    do
    {
        pAstNode = RTListGetFirst(&ListFree, VDSCRIPTASTCORE, ListNode);
        RTListNodeRemove(&pAstNode->ListNode);

        switch (pAstNode->enmClass)
        {
            case VDSCRIPTASTCLASS_FUNCTION:
            {
                PVDSCRIPTASTFN pFn = (PVDSCRIPTASTFN)pAstNode;

                if (pFn->pRetType)
                    RTListAppend(&ListFree, &pFn->pRetType->Core.ListNode);
                if (pFn->pFnIde)
                    RTListAppend(&ListFree, &pFn->pFnIde->Core.ListNode);

                /* Put argument list on the to free list. */
                while (!RTListIsEmpty(&pFn->ListArgs))
                {
                    PVDSCRIPTASTCORE pArg = RTListGetFirst(&pFn->ListArgs, VDSCRIPTASTCORE, ListNode);
                    RTListNodeRemove(&pArg->ListNode);
                    RTListAppend(&ListFree, &pArg->ListNode);
                }

                /* Put compound statement onto the list. */
                RTListAppend(&ListFree, &pFn->pCompoundStmts->Core.ListNode);
                break;
            }
            case VDSCRIPTASTCLASS_FUNCTIONARG:
            {
                PVDSCRIPTASTFNARG pAstNodeArg = (PVDSCRIPTASTFNARG)pAstNode;
                if (pAstNodeArg->pType)
                    RTListAppend(&ListFree, &pAstNodeArg->pType->Core.ListNode);
                if (pAstNodeArg->pArgIde)
                    RTListAppend(&ListFree, &pAstNodeArg->pArgIde->Core.ListNode);
                break;
            }
            case VDSCRIPTASTCLASS_IDENTIFIER:
                break;
            case VDSCRIPTASTCLASS_DECLARATION:
            case VDSCRIPTASTCLASS_TYPENAME:
            {
                AssertMsgFailed(("TODO\n"));
                break;
            }
            case VDSCRIPTASTCLASS_STATEMENT:
            {
                vdScriptAstNodeStatmentPutOnFreeList(&ListFree, pAstNode);
                break;
            }
            case VDSCRIPTASTCLASS_EXPRESSION:
            {
                vdScriptAstNodeExpressionPutOnFreeList(&ListFree, pAstNode);
                break;
            }
            case VDSCRIPTASTCLASS_INVALID:
            default:
                AssertMsgFailedReturnVoid(("Invalid AST node class given %d\n", pAstNode->enmClass));
        }

        RTMemFree(pAstNode);
    } while (!RTListIsEmpty(&ListFree));

}

DECLHIDDEN(PVDSCRIPTASTCORE) vdScriptAstNodeAlloc(VDSCRIPTASTCLASS enmClass)
{
    size_t cbAlloc = 0;

    switch (enmClass)
    {
        case VDSCRIPTASTCLASS_FUNCTION:
            cbAlloc = sizeof(VDSCRIPTASTFN);
            break;
        case VDSCRIPTASTCLASS_FUNCTIONARG:
            cbAlloc = sizeof(VDSCRIPTASTFNARG);
            break;
        case VDSCRIPTASTCLASS_DECLARATION:
            cbAlloc = sizeof(VDSCRIPTASTDECL);
            break;
        case VDSCRIPTASTCLASS_STATEMENT:
            cbAlloc = sizeof(VDSCRIPTASTSTMT);
            break;
        case VDSCRIPTASTCLASS_EXPRESSION:
            cbAlloc = sizeof(VDSCRIPTASTEXPR);
            break;
        case VDSCRIPTASTCLASS_TYPENAME:
            cbAlloc = sizeof(VDSCRIPTASTTYPENAME);
            break;
        case VDSCRIPTASTCLASS_IDENTIFIER:
        case VDSCRIPTASTCLASS_INVALID:
        default:
            AssertMsgFailedReturn(("Invalid AST node class given %d\n", enmClass), NULL);
    }

    PVDSCRIPTASTCORE pAstNode = (PVDSCRIPTASTCORE)RTMemAllocZ(cbAlloc);
    if (pAstNode)
    {
        pAstNode->enmClass = enmClass;
        RTListInit(&pAstNode->ListNode);
    }

    return pAstNode;
}

DECLHIDDEN(PVDSCRIPTASTIDE) vdScriptAstNodeIdeAlloc(size_t cchIde)
{
    PVDSCRIPTASTIDE pAstNode = (PVDSCRIPTASTIDE)RTMemAllocZ(RT_UOFFSETOF_DYN(VDSCRIPTASTIDE, aszIde[cchIde + 1]));
    if (pAstNode)
    {
        pAstNode->Core.enmClass = VDSCRIPTASTCLASS_IDENTIFIER;
        RTListInit(&pAstNode->Core.ListNode);
    }

    return pAstNode;
}