Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 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
7 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:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 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;
7 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;
7 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 "calculus.h"
63
#include "check.h"
64
#include "common.h"
65
#include "error.h"
66
#include "output.h"
67
#include "type_ops.h"
68
#include "xalloc.h"
69
 
70
 
71
/*
7 7u83 72
 *  LIST OF ALL NAMES
73
 *
74
 *  This variable contains a list of all the names defined within the algebra.
75
 */
2 7u83 76
 
7 7u83 77
static NAME *all_names = NULL;
2 7u83 78
 
79
 
80
/*
7 7u83 81
 *  ARRAY OF NAME ERRORS
82
 *
83
 *  This array gives all the errors associated with the various name types.
84
 */
2 7u83 85
 
7 7u83 86
static char *name_error[] = {
2 7u83 87
    "Type name %s",
88
    "Auxilliary type name %s",
89
    "Enumeration constant %s.%s",
90
    "Structure component %s.%s",
91
    "Union component %s.%s",
92
    "Union field component %s.%s.%s",
93
    "Union map %s.%s",
94
    "Union map argument %s.%s"
7 7u83 95
};
2 7u83 96
 
97
 
98
/*
7 7u83 99
 * LOOK UP A NAME
100
 *
101
 * This routine looks up the name with type n and components a, b and c in
102
 * the list of all names.
103
 */
2 7u83 104
 
7 7u83 105
static NAME *
106
find_name(int n, char *a, char *b, char *c)
2 7u83 107
{
7 7u83 108
    NAME *p;
109
    for (p = all_names; p != NULL; p = p->next) {
110
	if (p->type == n && streq(p->text[0], a) &&
111
	     streq(p->text[1], b) && streq(p->text[2], c)) {
112
	    return(p);;
2 7u83 113
	}
114
    }
7 7u83 115
    return(NULL);
2 7u83 116
}
117
 
118
 
119
/*
7 7u83 120
 * CREATE A NAME
121
 *
122
 * This routine adds the name with type n and components a, b and c to the
123
 * list of all names.
124
 */
2 7u83 125
 
7 7u83 126
static void
127
make_name(int n, char *a, char *b, char *c)
2 7u83 128
{
7 7u83 129
    static int names_left = 0;
130
    static NAME *names_free = NULL;
131
    NAME *p = find_name(n, a, b, c);
132
    if (p) {
133
	char buffer[1000];
134
	sprintf_v(buffer, name_error[n], a, b, c);
135
	error(ERROR_SERIOUS, "%s already defined (at %s, line %d)",
136
		buffer, p->file, p->line);
137
	return;
2 7u83 138
    }
7 7u83 139
    if (names_left == 0) {
140
	names_left = 1000;
141
	names_free = xmalloc_nof(NAME, names_left);
2 7u83 142
    }
7 7u83 143
    p = names_free + (--names_left);
144
    p->type = n;
145
    p->text[0] = a;
146
    p->text[1] = b;
147
    p->text[2] = c;
148
    p->file = crt_file_name;
149
    p->line = crt_line_no;
150
    p->next = all_names;
151
    all_names = p;
152
    return;
2 7u83 153
}
154
 
155
 
156
/*
7 7u83 157
 * EXTRACT NAMES FROM AN IDENTIFIER
158
 *
159
 * This routine assigns the names from the identifier id into na and nb.
160
 * It also sets the current file position to the location of id.
161
 */
2 7u83 162
 
7 7u83 163
static void
164
split_id(CLASS_ID_P id, char **na, char **nb)
2 7u83 165
{
7 7u83 166
    *na = DEREF_string(cid_name(id));
167
    *nb = DEREF_string(cid_name_aux(id));
168
    crt_file_name = DEREF_string(cid_file(id));
169
    crt_line_no = DEREF_int(cid_line(id));
170
    return;
2 7u83 171
}
172
 
173
 
174
/*
7 7u83 175
 * CHECK FOR NAME CLASHES
176
 *
177
 * This routine scans through the entire algebra definition checking for
178
 * name clashes.  Type names are only checked if c is true.
179
 */
2 7u83 180
 
7 7u83 181
void
182
check_names(int c)
2 7u83 183
{
7 7u83 184
    CLASS_ID_P id;
185
    char *na, *nb;
186
    char *empty = "";
187
    int line = crt_line_no;
188
    CONST char *file = crt_file_name;
189
    all_names = NULL;
2 7u83 190
 
191
    LOOP_PRIMITIVE {
7 7u83 192
	id = DEREF_ptr(prim_id(CRT_PRIMITIVE));
193
	split_id(id, &na, &nb);
194
	if (c)make_name(NAME_TYPE, na, empty, empty);
195
	make_name(NAME_TYPE_AUX, nb, empty, empty);
2 7u83 196
    }
197
 
198
    LOOP_IDENTITY {
7 7u83 199
	id = DEREF_ptr(ident_id(CRT_IDENTITY));
200
	split_id(id, &na, &nb);
201
	if (c)make_name(NAME_TYPE, na, empty, empty);
202
	make_name(NAME_TYPE_AUX, nb, empty, empty);
2 7u83 203
    }
204
 
205
    LOOP_ENUM {
7 7u83 206
	id = DEREF_ptr(en_id(CRT_ENUM));
207
	split_id(id, &na, &nb);
208
	if (c)make_name(NAME_TYPE, na, empty, empty);
209
	make_name(NAME_TYPE_AUX, nb, empty, empty);
2 7u83 210
	LOOP_ENUM_CONST {
7 7u83 211
	    char *ne = DEREF_string(ec_name(CRT_ECONST));
212
	    make_name(NAME_ENUM_CONST, na, ne, empty);
2 7u83 213
	}
214
    }
215
 
216
    LOOP_STRUCTURE {
7 7u83 217
	id = DEREF_ptr(str_id(CRT_STRUCTURE));
218
	split_id(id, &na, &nb);
219
	if (c)make_name(NAME_TYPE, na, empty, empty);
220
	make_name(NAME_TYPE_AUX, nb, empty, empty);
2 7u83 221
	LOOP_STRUCTURE_COMPONENT {
7 7u83 222
	    char *nc = DEREF_string(cmp_name(CRT_COMPONENT));
223
	    make_name(NAME_STRUCT_CMP, na, nc, empty);
2 7u83 224
	}
225
    }
226
 
227
    LOOP_UNION {
7 7u83 228
	id = DEREF_ptr(un_id(CRT_UNION));
229
	split_id(id, &na, &nb);
230
	if (c)make_name(NAME_TYPE, na, empty, empty);
231
	make_name(NAME_TYPE_AUX, nb, empty, empty);
2 7u83 232
	LOOP_UNION_COMPONENT {
7 7u83 233
	    char *nc = DEREF_string(cmp_name(CRT_COMPONENT));
234
	    make_name(NAME_UNION_CMP, na, nc, empty);
2 7u83 235
	}
236
	LOOP_UNION_FIELD {
7 7u83 237
	    char *nf = DEREF_string(fld_name(CRT_FIELD));
238
	    make_name(NAME_UNION_CMP, na, nf, empty);
2 7u83 239
	    LOOP_FIELD_COMPONENT {
7 7u83 240
		char *nc = DEREF_string(cmp_name(CRT_COMPONENT));
241
		make_name(NAME_FIELD_CMP, na, nf, nc);
2 7u83 242
	    }
243
	}
244
	LOOP_UNION_MAP {
7 7u83 245
	    char *nm = DEREF_string(map_name(CRT_MAP));
246
	    make_name(NAME_MAP, na, nm, empty);
2 7u83 247
	    LOOP_MAP_ARGUMENT {
7 7u83 248
		char *np = DEREF_string(arg_name(CRT_ARGUMENT));
249
		make_name(NAME_MAP_ARG, na, nm, np);
2 7u83 250
	    }
251
	}
252
    }
253
 
7 7u83 254
    crt_file_name = file;
255
    crt_line_no = line;
256
    return;
2 7u83 257
}