summaryrefslogtreecommitdiff
path: root/prepare.c
blob: 174a1794f47d95074f163cba508d8383e70258c6 (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
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
/* Oort
 * Copyright 2007, Soren Sandmann Pedersen
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "ast.h"

#include <stdlib.h>

/*
 * This pass does various stuff that doesn't really belong elsewhere
 *   - it hooks up parent pointers
 *   - it weeds out break/continue/return in wrong places
 *   - it gathers information about definitions
 */

typedef struct info_t info_t;
struct info_t
{
    GPtrArray       *functions;
    GPtrArray       *classes;
};

static gboolean prepare_statement (ast_statement_t *statement,
				   ast_t           *parent,
				   info_t          *info);
static gboolean prepare_definition (ast_definition_t *definition,
				    ast_t            *parent,
				    info_t           *info);

static void
set_parent (ast_t *ast, ast_t *parent)
{
    ast_ensure_ast (ast);

    ast->common.parent = parent;

    if (parent)
    {
	ast_ensure_ast (parent);

	g_queue_push_tail (parent->common.children, ast);
    }
}

static gboolean
prepare_void_type (ast_type_spec_t *type_spec)
{
    ast_t *parent = ((ast_t *)type_spec)->common.parent;

    if (ast_is (parent, AST_DEFINITION, AST_FUNCTION_DEFINITION)	&&
	parent->definition.function.return_type == type_spec)
    {
	return TRUE;
    }

    if (ast_is (parent, AST_TYPE_SPEC, AST_FUNCTION_TYPE)		&&
	parent->type_spec.function.return_ == type_spec)
    {
	return TRUE;
    }

    return report_error (
	"void is allowed only as the return type of a function\n");
}

static gboolean
prepare_type_spec (ast_type_spec_t *type_spec,
		   ast_t           *parent,
		   info_t          *info)
{
    int i;

    set_parent ((ast_t *)type_spec, parent);
    parent = ((ast_t *)type_spec);

    switch (type_spec->common.type)
    {
    case AST_FUNCTION_TYPE:
	if (!prepare_type_spec (type_spec->function.return_, parent, info))
	    return FALSE;
	for (i = 0; type_spec->function.args[i] != NULL; ++i)
	{
	    if (!prepare_type_spec (type_spec->function.args[i], parent, info))
		return FALSE;
	}
	break;
    case AST_ARRAY_TYPE:
	if (!prepare_type_spec (type_spec->array.child_type, parent, info))
	    return FALSE;
	break;
    case AST_VOID_TYPE:
	if (!prepare_void_type (type_spec))
	    return FALSE;
	break;
    case AST_LABEL_TYPE:
    case AST_OBJECT_TYPE:
    case AST_INT32_TYPE:
    case AST_DOUBLE_TYPE:
    case AST_BOOL_TYPE:
    case AST_IDENTIFIER_TYPE:
    case AST_NULL_TYPE:
    case AST_STRING_TYPE:
    case AST_INFERRED_TYPE:
	break;
    }

    return TRUE;
}

static gboolean
prepare_function (ast_function_definition_t *function,
		  ast_t                     *parent,
		  info_t                    *info)
{
    int i;

    g_ptr_array_add (info->functions, function);

    if (!prepare_type_spec (function->return_type, parent, info))
	return FALSE;

    for (i = 0; function->parameters[i]; ++i)
    {
	ast_variable_definition_t *parameter;

	parameter = function->parameters[i];

	parameter->parameter = TRUE;
	
	if (!prepare_definition ((ast_definition_t *)parameter, parent, info))
	    return FALSE;
    }

    if (!prepare_statement (function->body, parent, info))
	return FALSE;

    return TRUE;
}

static gboolean
prepare_class_body (ast_statement_t        *statement,
		    ast_class_definition_t *class)
{
    switch (statement->common.type)
    {
    case AST_BLOCK_STATEMENT:
    case AST_PRINT_STATEMENT:
    case AST_LOOP_STATEMENT:
    case AST_IF_STATEMENT:
    case AST_SWITCH_STATEMENT:
    case AST_EXPRESSION_STATEMENT:
    case AST_RETURN_STATEMENT:
    case AST_GOTO_STATEMENT:
    case AST_BREAK_STATEMENT:
    case AST_CONTINUE_STATEMENT:
    case AST_LABEL_STATEMENT:
	return report_error ("Illegal statement in class\n");
	break;

    case AST_DEFINITION_STATEMENT:
    case AST_EMPTY_STATEMENT:
	break;

    case AST_COMPOUND_STATEMENT:
	if (!prepare_class_body (statement->compound.first, class))
	    return FALSE;
	if (!prepare_class_body (statement->compound.second, class))
	    return FALSE;
	break;
    }

    return TRUE;
}

static gboolean
prepare_class (ast_class_definition_t *class,
	       ast_t                  *parent,
	       info_t                 *info)
{
    g_ptr_array_add (info->classes, class);

    if (!prepare_class_body (class->statement, class))
	return FALSE;

    if (!prepare_statement (class->statement, parent, info))
	return FALSE;

    return TRUE;
}

static gboolean
prepare_definition (ast_definition_t *definition,
		    ast_t            *parent,
		    info_t           *info)
{
    ast_t *ast = (ast_t *)definition;
    ast_t *enclosing_class;
    ast_t *enclosing_function;

    set_parent (ast, parent);
    parent = ast;

    switch (definition->common.type)
    {
    case AST_VARIABLE_DEFINITION:
	if (!prepare_type_spec (definition->variable.type_spec, parent, info))
	    return FALSE;

	enclosing_class = (ast_t *)ast_enclosing_class (ast);
	enclosing_function = (ast_t *)ast_enclosing_function (ast);
	if (enclosing_class &&
	    (!enclosing_function ||
	     ast_encloses (enclosing_function, enclosing_class)))
	{
	    definition->variable.field = TRUE;
	}

	g_assert (ast->common.children->head);
	break;

    case AST_FUNCTION_DEFINITION:
	if (!prepare_function (&definition->function, parent, info))
	    return FALSE;
	break;

    case AST_CLASS_DEFINITION:
	if (!prepare_class (&definition->class, parent, info))
	    return FALSE;
	break;

    case AST_TYPE_DEFINITION:
	if (!prepare_type_spec (definition->type.type_spec, parent, info))
	    return FALSE;
	break;

    case AST_LABEL_DEFINITION:
	break;
    }

    return TRUE;
}

static gboolean
prepare_expression (ast_expression_t *expr,
		    ast_t            *parent,
		    info_t           *info)
{
    int i;
    ast_t *ast = (ast_t *)expr;

    set_parent (ast, parent);
    parent = ast;

    switch (expr->common.type)
    {
    case AST_INT_LITERAL_EXPRESSION:
    case AST_FLOAT_LITERAL_EXPRESSION:
    case AST_BOOL_LITERAL_EXPRESSION:
    case AST_STRING_LITERAL_EXPRESSION:
    case AST_NULL_EXPRESSION:
    case AST_VOID_EXPRESSION:
    case AST_IDENTIFIER_EXPRESSION:
    case AST_THIS_EXPRESSION:
	break;

    case AST_UNARY_EXPRESSION:
	if (!prepare_expression (expr->unary.expr, parent, info))
	    return FALSE;
	break;

    case AST_BINARY_EXPRESSION:
	if (!prepare_expression (expr->binary.left, parent, info))
	    return FALSE;
	if (!prepare_expression (expr->binary.right, parent, info))
	    return FALSE;
	break;

    case AST_TERNARY_EXPRESSION:
	if (!prepare_expression (expr->ternary.cond, parent, info))
	    return FALSE;
	if (!prepare_expression (expr->ternary.then_expr, parent, info))
	    return FALSE;
	if (!prepare_expression (expr->ternary.else_expr, parent, info))
	    return FALSE;
	break;

    case AST_LAMBDA_EXPRESSION:
	if (!prepare_definition ((ast_definition_t *)expr->lambda.function,
				 parent, info))
	{
	    return FALSE;
	}
	break;

    case AST_STATEMENT_EXPRESSION:
	if (!prepare_statement (expr->statement.statement, parent, info))
	    return FALSE;
	if (!prepare_expression (expr->statement.expression, parent, info))
	    return FALSE;
	break;

    case AST_NEW_EXPRESSION:
	if (!prepare_type_spec (expr->new.type, parent, info))
	    return FALSE;
	for (i = 0; expr->new.args[i]; ++i)
	{
	    if (!prepare_expression (expr->new.args[i], parent, info))
		return FALSE;
	}
	break;

    case AST_BLOCK_EXPRESSION:
	if (!prepare_expression (expr->block.body, parent, info))
	    return FALSE;
	break;

    case AST_DEFINITION_EXPRESSION:
	if (!prepare_definition (expr->definition.definition, parent, info))
	    return FALSE;
	if (!prepare_expression (expr->definition.initializer, parent, info))
	    return FALSE;
	break;

    case AST_DOT_EXPRESSION:
	if (!prepare_expression (expr->dot.left, parent, info))
	    return FALSE;
	break;

    case AST_INDEX_EXPRESSION:
	if (!prepare_expression (expr->index.left, parent, info))
	    return FALSE;
	if (!prepare_expression (expr->index.right, parent, info))
	    return FALSE;
	break;

    case AST_CALL_EXPRESSION:
	if (!prepare_expression (expr->call.callee, parent, info))
	    return FALSE;
	for (i = 0; expr->call.args[i]; ++i)
	{
	    if (!prepare_expression (expr->call.args[i], parent, info))
		return FALSE;
	}
	break;
    }

    return TRUE;
}

static gboolean
prepare_case (ast_case_t *case_,
	      ast_t      *parent,
	      info_t     *info)
{
    ast_t *ast = (ast_t *)case_;
    set_parent (ast, parent);
    parent = ast;

    switch (case_->common.type)
    {
    case AST_EXPRESSION_CASE:
	if (!prepare_expression (case_->expression.expr, parent, info))
	    return FALSE;
	break;

    case AST_DEFAULT_CASE:
	break;
    }

    if (!prepare_statement (case_->common.statement, parent, info))
	return FALSE;

    return TRUE;
}

static gboolean
prepare_statement (ast_statement_t *statement,
		   ast_t           *parent,
		   info_t          *info)
{
    int i;
    ast_t *enclosing, *function;
    ast_t *ast = (ast_t *)statement;
    set_parent (ast, parent);
    parent = ast;

    switch (statement->common.type)
    {
    case AST_DEFINITION_STATEMENT:
	if (!prepare_definition (statement->definition.def, parent, info))
	    return FALSE;
	break;

    case AST_LOOP_STATEMENT:
	if (!prepare_statement (statement->loop.init, parent, info))
	    return FALSE;
	if (!prepare_expression (statement->loop.condition, parent, info))
	    return FALSE;
	if (!prepare_statement (statement->loop.body, parent, info))
	    return FALSE;
	if (!prepare_statement (statement->loop.increment, parent, info))
	    return FALSE;
	break;

    case AST_IF_STATEMENT:
	if (!prepare_expression (statement->if_.condition, parent, info))
	    return FALSE;
	if (!prepare_statement (statement->if_.then_part, parent, info))
	    return FALSE;
	if (!prepare_statement (statement->if_.else_part, parent, info))
	    return FALSE;
	break;

    case AST_SWITCH_STATEMENT:
	if (!prepare_expression (statement->switch_.condition, parent, info))
	    return FALSE;
	for (i = 0; statement->switch_.cases[i] != NULL; ++i)
	{
	    if (!prepare_case (statement->switch_.cases[i], parent, info))
		return FALSE;
	}
	break;

    case AST_COMPOUND_STATEMENT:
	if (!prepare_statement (statement->compound.first, parent, info))
	    return FALSE;
	if (!prepare_statement (statement->compound.second, parent, info))
	    return FALSE;
	break;

    case AST_BLOCK_STATEMENT:
	if (!prepare_statement (statement->block.body, parent, info))
	    return FALSE;
	break;

    case AST_PRINT_STATEMENT:
	for (i = 0; statement->print.exprs[i]; ++i)
	{
	    if (!prepare_expression (statement->print.exprs[i], parent, info))
		return FALSE;
	}
	break;

    case AST_EXPRESSION_STATEMENT:
	if (!prepare_expression (statement->expression.expr, parent, info))
	    return FALSE;
	break;

    case AST_RETURN_STATEMENT:
	if (!prepare_expression (statement->return_.expr, parent, info))
	    return FALSE;
	if (!ast_enclosing_function (ast))
	    return report_error ("return outside of function\n");
	break;

    case AST_BREAK_STATEMENT:
    case AST_CONTINUE_STATEMENT:
	if (!(enclosing = (ast_t *)ast_enclosing_loop (ast)))
	{
	    if (statement->common.type == AST_CONTINUE_STATEMENT)
	    {
		return report_error ("continue not inside for, while, or do\n");
	    }
	    else
	    {
		if (!(enclosing = (ast_t *)ast_enclosing_switch (ast)))
		{
		    return report_error (
			"break not inside for, while, do, or switch\n");
		}
	    }
	}

	if ((function = (ast_t *)ast_enclosing_function (ast)))
	{
	    if (ast_encloses (enclosing, function))
	    {
		/* FIXME: We could actually allow this since we support
		 * gotos out of the current function; the question is
		 * whether that would be more confusing than useful.
		 */
		return report_error (
		    "break or continue cannot be used with a do, while, for, "
		    "or switch which is outside the current function\n");
	    }
	}
	break;
    case AST_GOTO_STATEMENT:
	if (!prepare_expression (statement->goto_.target, parent, info))
	    return FALSE;
	break;
    case AST_LABEL_STATEMENT:
	statement->label.definition = ast_definition_new_label (
	    statement->label.label, &(statement->label));

	if (!prepare_definition (statement->label.definition, parent, info))
	    return FALSE;
	break;

    case AST_EMPTY_STATEMENT:
	break;
    }

    return TRUE;
}

gboolean
prepare (ast_t *ast)
{
    info_t info;
    ast_program_t *program = &ast->program;

    set_parent (ast, NULL);

    info.functions = g_ptr_array_new ();
    info.classes = g_ptr_array_new ();

    if (!prepare_statement (program->statement, ast, &info))
	return FALSE;

    g_ptr_array_add (info.functions, NULL);
    g_ptr_array_add (info.classes, NULL);

    program->functions =
	(ast_function_definition_t **)info.functions->pdata;

    return TRUE;
}