summaryrefslogtreecommitdiff
path: root/mark-constant.c
blob: e1f621a2ec2e00c8ed8d7e3d43862e9c12db3ebd (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
/* 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>

/* Mark constant expressions as such */

gboolean
mark_constants (ast_t *ast)
{
    ast_expression_t *expr;
    gboolean constant;
    value_t *result;
    GList *list;

    for (list = ast->common.children->head; list; list = list->next)
	mark_constants (list->data);
    
    if (ast->common.type != AST_EXPRESSION)
	return TRUE;

    constant = FALSE;

    expr = &ast->expression;
    result = &expr->common.constant_value;

    switch (expr->common.type)
    {
    case AST_INT_LITERAL_EXPRESSION:
	result->int32_val = expr->int_literal.value;
	constant = TRUE;
	break;

    case AST_FLOAT_LITERAL_EXPRESSION:
	result->double_val = expr->float_literal.value;
	constant = TRUE;
	break;
	
    case AST_BOOL_LITERAL_EXPRESSION:
	result->bool_val = expr->bool_literal.value;
	constant = TRUE;
	break;

    case AST_STRING_LITERAL_EXPRESSION:
	result->pointer_val = expr->string_literal.value;
	constant = TRUE;
	break;

    case AST_NULL_EXPRESSION:
	result->pointer_val = NULL;
	constant = TRUE;
	break;

    case AST_IDENTIFIER_EXPRESSION:
	/* FIXME: maybe check if it's a constant, particularly
	 * when we add enums
	 */
	break;

    case AST_LAMBDA_EXPRESSION:
    case AST_CALL_EXPRESSION:
    case AST_STATEMENT_EXPRESSION:
    case AST_NEW_EXPRESSION:
    case AST_DOT_EXPRESSION:
    case AST_THIS_EXPRESSION:
    case AST_VOID_EXPRESSION:
    case AST_INDEX_EXPRESSION:
	break;

    case AST_DEFINITION_EXPRESSION:
	/* FIXME: It might actually be useful to allow this */
	break;

    case AST_BLOCK_EXPRESSION:
	if (expr->block.body->common.constant)
	{
	    *result = expr->block.body->common.constant_value;
	    constant = TRUE;
	}
	break;

    case AST_UNARY_EXPRESSION:
	if (expr->unary.expr->common.constant)
	{
	    evaluate_unary_operator (expr->unary.operator,
				     expr->unary.expr->common.type_spec,
				     &expr->unary.expr->common.constant_value,
				     expr->common.type_spec, result);
	    constant = TRUE;
	}
	break;

    case AST_BINARY_EXPRESSION:
	if (expr->binary.left->common.constant		&&
	    expr->binary.right->common.constant)
	{
	    value_t *left = &expr->binary.left->common.constant_value;
	    value_t *right = &expr->binary.right->common.constant_value;

	    evaluate_binary_operator (
		expr->binary.operator,
		expr->binary.left->common.type_spec, left,
		expr->binary.right->common.type_spec, right,
		expr->common.type_spec, result);

	    constant = TRUE;
	}
	break;

    case AST_TERNARY_EXPRESSION:
	/* FIXME: we could require only the taken branch to be constant */

	if (expr->ternary.cond->common.constant		&&
	    expr->ternary.then_expr->common.constant	&&
	    expr->ternary.else_expr->common.constant)
	{
	    if (expr->ternary.cond->common.constant_value.bool_val)
		*result = expr->ternary.then_expr->common.constant_value;
	    else
		*result = expr->ternary.else_expr->common.constant_value;

	    constant = TRUE;
	}
	break;
    }

    expr->common.constant = constant;

    return TRUE;
}