Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
6 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
6 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
6 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "c_types.h"
63
#include "exp_ops.h"
64
#include "error.h"
65
#include "catalog.h"
66
#include "operator.h"
67
#include "quality.h"
68
#include "syntax.h"
69
 
70
 
71
/*
72
    CHECKING CONTROL FLAG
73
 
74
    This flag may be set to true to temporarily disable the checks in
75
    this module.
76
*/
77
 
6 7u83 78
int suppress_quality = 0;
2 7u83 79
 
80
 
81
/*
82
    TABLE OF OPERATIONS
83
 
84
    This table gives the list of operations checked by check_paren.  They
85
    are listed in order of decreasing precedence.  Calls to check_paren
86
    give an entry point into this table beyond which checks are applied.
87
    For example, left shift operations are checked from PAREN_PLUS which
88
    detects 'a << b + c', 'a << b - c', 'a + b << c' and 'a - b << c'.
89
*/
90
 
91
static struct {
6 7u83 92
	unsigned tag;
93
	int op;
94
} paren_ops[] = {
95
	{ exp_or_tag, lex_or_H1 },	/* PAREN_OR */
96
	{ exp_xor_tag, lex_xor_H1 },	/* PAREN_XOR */
97
	{ exp_and_tag, lex_and_H1 },	/* PAREN_AND */
98
	{ exp_compare_tag, lex_eq },	/* PAREN_EQUALITY */
99
	{ exp_test_tag, lex_eq },	/* PAREN_RELATION */
100
	{ exp_plus_tag, lex_plus },	/* PAREN_PLUS */
101
	{ exp_minus_tag, lex_minus }	/* PAREN_MINUS */
102
};
2 7u83 103
 
104
 
105
/*
106
    FIND AN EXPRESSION TAG
107
 
108
    This routine finds the tag associated with the expression e for unusual
109
    parenthesis analysis etc.  Note that an integer constant expression may
110
    have been evaluated to give a simple integer constant, however the top
111
    level operation giving rise to this constant is held in the etag field.
112
*/
113
 
6 7u83 114
static unsigned
115
exp_tag (EXP e)
2 7u83 116
{
6 7u83 117
	unsigned tag = TAG_exp(e);
118
	if (tag == exp_int_lit_tag) {
119
		/* Allow for evaluated constants */
120
		tag = DEREF_unsigned(exp_int_lit_etag(e));
121
	}
122
	return (tag);
2 7u83 123
}
124
 
125
 
126
/*
127
    CHECK FOR DUBIOUS SHIFTS AND BIT OPERATIONS
128
 
129
    For various pairs of operations, op1 and op2, the resolution of the
130
    expression 'a op1 b op2 c' to '( a op1 b ) op2 c' or 'a op1 ( b op2 c )'
131
    is not obvious.  This routine checks whether the expression 'a op b'
132
    is of this form, checking the operations starting from position n in
133
    the table paren_ops.
134
*/
135
 
6 7u83 136
void
137
check_paren(int n, int op, EXP a, EXP b)
2 7u83 138
{
6 7u83 139
	if (!suppress_quality) {
140
		int i;
2 7u83 141
 
6 7u83 142
		/* Check first operand */
143
		unsigned tag = exp_tag(a);
144
		for (i = n; i < array_size(paren_ops); i++) {
145
			if (tag == paren_ops[i].tag) {
146
				int op1 = paren_ops[i].op;
147
				if (op1 == lex_eq) {
148
					op1 = op_token(a, op1);
149
				}
150
				report(crt_loc, ERR_expr_paren_left(op1, op));
151
				break;
152
			}
153
		}
2 7u83 154
 
6 7u83 155
		/* Check second operand */
156
		tag = exp_tag(b);
157
		for (i = n; i < array_size(paren_ops); i++) {
158
			if (tag == paren_ops[i].tag) {
159
				int op2 = paren_ops[i].op;
160
				if (op2 == lex_eq) {
161
					op2 = op_token(b, op2);
162
				}
163
				report(crt_loc, ERR_expr_paren_right(op, op2));
164
				break;
165
			}
166
		}
2 7u83 167
	}
6 7u83 168
	return;
2 7u83 169
}
170
 
171
 
172
/*
173
    CHECK FOR DUBIOUS RELATIONS
174
 
175
    This routine checks for dubious relations such as 'a < b < c' which
176
    do not have their mathematical meaning.
177
*/
178
 
6 7u83 179
void
180
check_relation(int op, EXP a, EXP b)
2 7u83 181
{
6 7u83 182
	if (!suppress_quality) {
183
		/* Check first operand */
184
		unsigned tag = exp_tag(a);
185
		if (tag == exp_compare_tag) {
186
			int tst = op_token(a, op);
187
			report(crt_loc, ERR_expr_rel_paren(tst, op));
188
		} else if (tag == exp_test_tag) {
189
			int tst = op_token(a, op);
190
			report(crt_loc, ERR_expr_rel_paren(tst, op));
191
		}
2 7u83 192
 
6 7u83 193
		/* Check second operand */
194
		tag = exp_tag(b);
195
		if (tag == exp_compare_tag) {
196
			int tst = op_token(b, op);
197
			report(crt_loc, ERR_expr_rel_paren(op, tst));
198
		} else if (tag == exp_test_tag) {
199
			int tst = op_token(b, op);
200
			report(crt_loc, ERR_expr_rel_paren(op, tst));
201
		}
2 7u83 202
	}
6 7u83 203
	return;
2 7u83 204
}
205
 
206
 
207
/*
208
    CHECK FOR DUBIOUS LOGICAL EXPRESSIONS
209
 
210
    The resolution of 'a && b || c' to '( a && b ) || c' is odd.  This
211
    routine checks a logical or expression to see if it is of this form.
212
*/
213
 
6 7u83 214
void
215
check_logic(EXP a, EXP b)
2 7u83 216
{
6 7u83 217
	if (!suppress_quality) {
218
		if (exp_tag(a) == exp_log_and_tag) {
219
			int op1 = lex_logical_Hand_H1;
220
			int op2 = lex_logical_Hor_H1;
221
			report(crt_loc, ERR_expr_paren_left(op1, op2));
222
		}
223
		if (exp_tag(b) == exp_log_and_tag) {
224
			int op1 = lex_logical_Hor_H1;
225
			int op2 = lex_logical_Hand_H1;
226
			report(crt_loc, ERR_expr_paren_right(op1, op2));
227
		}
2 7u83 228
	}
6 7u83 229
	return;
2 7u83 230
}