Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
29
 
59
 
30
 
60
 
31
#include "config.h"
61
#include "config.h"
32
#include "calculus.h"
62
#include "calculus.h"
33
#include "check.h"
63
#include "check.h"
34
#include "common.h"
64
#include "common.h"
35
#include "error.h"
65
#include "error.h"
36
#include "output.h"
66
#include "output.h"
37
#include "type_ops.h"
67
#include "type_ops.h"
38
#include "xalloc.h"
68
#include "xalloc.h"
39
 
69
 
40
 
70
 
41
/*
71
/*
42
    LIST OF ALL NAMES
72
 *  LIST OF ALL NAMES
43
 
73
 *
44
    This variable contains a list of all the names defined within the algebra.
74
 *  This variable contains a list of all the names defined within the algebra.
45
*/
75
 */
46
 
76
 
47
static NAME *all_names = NULL ;
77
static NAME *all_names = NULL;
48
 
78
 
49
 
79
 
50
/*
80
/*
51
    ARRAY OF NAME ERRORS
81
 *  ARRAY OF NAME ERRORS
52
 
82
 *
53
    This array gives all the errors associated with the various name types.
83
 *  This array gives all the errors associated with the various name types.
54
*/
84
 */
55
 
85
 
56
static char *name_error [] = {
86
static char *name_error[] = {
57
    "Type name %s",
87
    "Type name %s",
58
    "Auxilliary type name %s",
88
    "Auxilliary type name %s",
59
    "Enumeration constant %s.%s",
89
    "Enumeration constant %s.%s",
60
    "Structure component %s.%s",
90
    "Structure component %s.%s",
61
    "Union component %s.%s",
91
    "Union component %s.%s",
62
    "Union field component %s.%s.%s",
92
    "Union field component %s.%s.%s",
63
    "Union map %s.%s",
93
    "Union map %s.%s",
64
    "Union map argument %s.%s"
94
    "Union map argument %s.%s"
65
} ;
95
};
66
 
96
 
67
 
97
 
68
/*
98
/*
69
    LOOK UP A NAME
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
 */
70
 
104
 
71
    This routine looks up the name with type n and components a, b and c in
-
 
72
    the list of all names.
-
 
73
*/
-
 
74
 
-
 
75
static NAME *find_name
105
static NAME *
76
    PROTO_N ( ( n, a, b, c ) )
-
 
77
    PROTO_T ( int n X char *a X char *b X char *c )
106
find_name(int n, char *a, char *b, char *c)
78
{
107
{
79
    NAME *p ;
108
    NAME *p;
80
    for ( p = all_names ; p != NULL ; p = p->next ) {
109
    for (p = all_names; p != NULL; p = p->next) {
81
	if ( p->type == n && streq ( p->text [0], a ) &&
110
	if (p->type == n && streq(p->text[0], a) &&
82
	     streq ( p->text [1], b ) && streq ( p->text [2], c ) ) {
111
	     streq(p->text[1], b) && streq(p->text[2], c)) {
83
	    return ( p ) ; ;
112
	    return(p);;
84
	}
113
	}
85
    }
114
    }
86
    return ( NULL ) ;
115
    return(NULL);
87
}
116
}
88
 
117
 
89
 
118
 
90
/*
119
/*
91
    CREATE A NAME
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
 */
92
 
125
 
93
    This routine adds the name with type n and components a, b and c to the
-
 
94
    list of all names.
-
 
95
*/
-
 
96
 
-
 
97
static void make_name
126
static void
98
    PROTO_N ( ( n, a, b, c ) )
-
 
99
    PROTO_T ( int n X char *a X char *b X char *c )
127
make_name(int n, char *a, char *b, char *c)
100
{
128
{
101
    static int names_left = 0 ;
129
    static int names_left = 0;
102
    static NAME *names_free = NULL ;
130
    static NAME *names_free = NULL;
103
    NAME *p = find_name ( n, a, b, c ) ;
131
    NAME *p = find_name(n, a, b, c);
104
    if ( p ) {
132
    if (p) {
105
	char buffer [1000] ;
133
	char buffer[1000];
106
	sprintf_v ( buffer, name_error [n], a, b, c ) ;
134
	sprintf_v(buffer, name_error[n], a, b, c);
107
	error ( ERROR_SERIOUS, "%s already defined (at %s, line %d)",
135
	error(ERROR_SERIOUS, "%s already defined (at %s, line %d)",
108
		buffer, p->file, p->line ) ;
136
		buffer, p->file, p->line);
109
	return ;
137
	return;
-
 
138
    }
-
 
139
    if (names_left == 0) {
-
 
140
	names_left = 1000;
-
 
141
	names_free = xmalloc_nof(NAME, names_left);
110
    }
142
    }
111
    if ( names_left == 0 ) {
-
 
112
	names_left = 1000 ;
-
 
113
	names_free = xmalloc_nof ( NAME, names_left ) ;
-
 
114
    }
-
 
115
    p = names_free + ( --names_left ) ;
143
    p = names_free + (--names_left);
116
    p->type = n ;
144
    p->type = n;
117
    p->text [0] = a ;
145
    p->text[0] = a;
118
    p->text [1] = b ;
146
    p->text[1] = b;
119
    p->text [2] = c ;
147
    p->text[2] = c;
120
    p->file = crt_file_name ;
148
    p->file = crt_file_name;
121
    p->line = crt_line_no ;
149
    p->line = crt_line_no;
122
    p->next = all_names ;
150
    p->next = all_names;
123
    all_names = p ;
151
    all_names = p;
124
    return ;
152
    return;
125
}
153
}
126
 
154
 
127
 
155
 
128
/*
156
/*
129
    EXTRACT NAMES FROM AN IDENTIFIER
157
 * EXTRACT NAMES FROM AN IDENTIFIER
130
 
158
 *
131
    This routine assigns the names from the identifier id into na and nb.
159
 * This routine assigns the names from the identifier id into na and nb.
132
    It also sets the current file position to the location of id.
160
 * It also sets the current file position to the location of id.
133
*/
161
 */
134
 
162
 
135
static void split_id
163
static void
136
    PROTO_N ( ( id, na, nb ) )
-
 
137
    PROTO_T ( CLASS_ID_P id X char **na X char **nb )
164
split_id(CLASS_ID_P id, char **na, char **nb)
138
{
165
{
139
    *na = DEREF_string ( cid_name ( id ) ) ;
166
    *na = DEREF_string(cid_name(id));
140
    *nb = DEREF_string ( cid_name_aux ( id ) ) ;
167
    *nb = DEREF_string(cid_name_aux(id));
141
    crt_file_name = DEREF_string ( cid_file ( id ) ) ;
168
    crt_file_name = DEREF_string(cid_file(id));
142
    crt_line_no = DEREF_int ( cid_line ( id ) ) ;
169
    crt_line_no = DEREF_int(cid_line(id));
143
    return ;
170
    return;
144
}
171
}
145
 
172
 
146
 
173
 
147
/*
174
/*
148
    CHECK FOR NAME CLASHES
175
 * CHECK FOR NAME CLASHES
149
 
176
 *
150
    This routine scans through the entire algebra definition checking for
177
 * This routine scans through the entire algebra definition checking for
151
    name clashes.  Type names are only checked if c is true.
178
 * name clashes.  Type names are only checked if c is true.
152
*/
179
 */
153
 
180
 
154
void check_names
181
void
155
    PROTO_N ( ( c ) )
-
 
156
    PROTO_T ( int c )
182
check_names(int c)
157
{
183
{
158
    CLASS_ID_P id ;
184
    CLASS_ID_P id;
159
    char *na, *nb ;
185
    char *na, *nb;
160
    char *empty = "" ;
186
    char *empty = "";
161
    int line = crt_line_no ;
187
    int line = crt_line_no;
162
    CONST char *file = crt_file_name ;
188
    CONST char *file = crt_file_name;
163
    all_names = NULL ;
189
    all_names = NULL;
164
 
190
 
165
    LOOP_PRIMITIVE {
191
    LOOP_PRIMITIVE {
166
	id = DEREF_ptr ( prim_id ( CRT_PRIMITIVE ) ) ;
192
	id = DEREF_ptr(prim_id(CRT_PRIMITIVE));
167
	split_id ( id, &na, &nb ) ;
193
	split_id(id, &na, &nb);
168
	if ( c ) make_name ( NAME_TYPE, na, empty, empty ) ;
194
	if (c)make_name(NAME_TYPE, na, empty, empty);
169
	make_name ( NAME_TYPE_AUX, nb, empty, empty ) ;
195
	make_name(NAME_TYPE_AUX, nb, empty, empty);
170
    }
196
    }
171
 
197
 
172
    LOOP_IDENTITY {
198
    LOOP_IDENTITY {
173
	id = DEREF_ptr ( ident_id ( CRT_IDENTITY ) ) ;
199
	id = DEREF_ptr(ident_id(CRT_IDENTITY));
174
	split_id ( id, &na, &nb ) ;
200
	split_id(id, &na, &nb);
175
	if ( c ) make_name ( NAME_TYPE, na, empty, empty ) ;
201
	if (c)make_name(NAME_TYPE, na, empty, empty);
176
	make_name ( NAME_TYPE_AUX, nb, empty, empty ) ;
202
	make_name(NAME_TYPE_AUX, nb, empty, empty);
177
    }
203
    }
178
 
204
 
179
    LOOP_ENUM {
205
    LOOP_ENUM {
180
	id = DEREF_ptr ( en_id ( CRT_ENUM ) ) ;
206
	id = DEREF_ptr(en_id(CRT_ENUM));
181
	split_id ( id, &na, &nb ) ;
207
	split_id(id, &na, &nb);
182
	if ( c ) make_name ( NAME_TYPE, na, empty, empty ) ;
208
	if (c)make_name(NAME_TYPE, na, empty, empty);
183
	make_name ( NAME_TYPE_AUX, nb, empty, empty ) ;
209
	make_name(NAME_TYPE_AUX, nb, empty, empty);
184
	LOOP_ENUM_CONST {
210
	LOOP_ENUM_CONST {
185
	    char *ne = DEREF_string ( ec_name ( CRT_ECONST ) ) ;
211
	    char *ne = DEREF_string(ec_name(CRT_ECONST));
186
	    make_name ( NAME_ENUM_CONST, na, ne, empty ) ;
212
	    make_name(NAME_ENUM_CONST, na, ne, empty);
187
	}
213
	}
188
    }
214
    }
189
 
215
 
190
    LOOP_STRUCTURE {
216
    LOOP_STRUCTURE {
191
	id = DEREF_ptr ( str_id ( CRT_STRUCTURE ) ) ;
217
	id = DEREF_ptr(str_id(CRT_STRUCTURE));
192
	split_id ( id, &na, &nb ) ;
218
	split_id(id, &na, &nb);
193
	if ( c ) make_name ( NAME_TYPE, na, empty, empty ) ;
219
	if (c)make_name(NAME_TYPE, na, empty, empty);
194
	make_name ( NAME_TYPE_AUX, nb, empty, empty ) ;
220
	make_name(NAME_TYPE_AUX, nb, empty, empty);
195
	LOOP_STRUCTURE_COMPONENT {
221
	LOOP_STRUCTURE_COMPONENT {
196
	    char *nc = DEREF_string ( cmp_name ( CRT_COMPONENT ) ) ;
222
	    char *nc = DEREF_string(cmp_name(CRT_COMPONENT));
197
	    make_name ( NAME_STRUCT_CMP, na, nc, empty ) ;
223
	    make_name(NAME_STRUCT_CMP, na, nc, empty);
198
	}
224
	}
199
    }
225
    }
200
 
226
 
201
    LOOP_UNION {
227
    LOOP_UNION {
202
	id = DEREF_ptr ( un_id ( CRT_UNION ) ) ;
228
	id = DEREF_ptr(un_id(CRT_UNION));
203
	split_id ( id, &na, &nb ) ;
229
	split_id(id, &na, &nb);
204
	if ( c ) make_name ( NAME_TYPE, na, empty, empty ) ;
230
	if (c)make_name(NAME_TYPE, na, empty, empty);
205
	make_name ( NAME_TYPE_AUX, nb, empty, empty ) ;
231
	make_name(NAME_TYPE_AUX, nb, empty, empty);
206
	LOOP_UNION_COMPONENT {
232
	LOOP_UNION_COMPONENT {
207
	    char *nc = DEREF_string ( cmp_name ( CRT_COMPONENT ) ) ;
233
	    char *nc = DEREF_string(cmp_name(CRT_COMPONENT));
208
	    make_name ( NAME_UNION_CMP, na, nc, empty ) ;
234
	    make_name(NAME_UNION_CMP, na, nc, empty);
209
	}
235
	}
210
	LOOP_UNION_FIELD {
236
	LOOP_UNION_FIELD {
211
	    char *nf = DEREF_string ( fld_name ( CRT_FIELD ) ) ;
237
	    char *nf = DEREF_string(fld_name(CRT_FIELD));
212
	    make_name ( NAME_UNION_CMP, na, nf, empty ) ;
238
	    make_name(NAME_UNION_CMP, na, nf, empty);
213
	    LOOP_FIELD_COMPONENT {
239
	    LOOP_FIELD_COMPONENT {
214
		char *nc = DEREF_string ( cmp_name ( CRT_COMPONENT ) ) ;
240
		char *nc = DEREF_string(cmp_name(CRT_COMPONENT));
215
		make_name ( NAME_FIELD_CMP, na, nf, nc ) ;
241
		make_name(NAME_FIELD_CMP, na, nf, nc);
216
	    }
242
	    }
217
	}
243
	}
218
	LOOP_UNION_MAP {
244
	LOOP_UNION_MAP {
219
	    char *nm = DEREF_string ( map_name ( CRT_MAP ) ) ;
245
	    char *nm = DEREF_string(map_name(CRT_MAP));
220
	    make_name ( NAME_MAP, na, nm, empty ) ;
246
	    make_name(NAME_MAP, na, nm, empty);
221
	    LOOP_MAP_ARGUMENT {
247
	    LOOP_MAP_ARGUMENT {
222
		char *np = DEREF_string ( arg_name ( CRT_ARGUMENT ) ) ;
248
		char *np = DEREF_string(arg_name(CRT_ARGUMENT));
223
		make_name ( NAME_MAP_ARG, na, nm, np ) ;
249
		make_name(NAME_MAP_ARG, na, nm, np);
224
	    }
250
	    }
225
	}
251
	}
226
    }
252
    }
227
 
253
 
228
    crt_file_name = file ;
254
    crt_file_name = file;
229
    crt_line_no = line ;
255
    crt_line_no = line;
230
    return ;
256
    return;
231
}
257
}