Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
/*** scope.c --- Scope stack ADT.
32
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
35
 *** Commentary:
36
 *
37
 * This file implements the scope stack manipulation routines.
38
 *
39
 *** Change Log:
40
 * $Log: scope.c,v $
41
 * Revision 1.1.1.1  1998/01/17  15:57:47  release
42
 * First version to be checked into rolling release.
43
 *
44
 * Revision 1.2  1994/12/15  09:59:02  smf
45
 * Brought into line with OSSG C Coding Standards Document, as per
46
 * "CR94_178.sid+tld-update".
47
 *
48
 * Revision 1.1.1.1  1994/07/25  16:04:42  smf
49
 * Initial import of SID 1.8 non shared files.
50
 *
51
**/
52
 
53
/****************************************************************************/
54
 
55
#include "scope.h"
56
#include "gen-errors.h"
57
#include "rule.h"
58
 
59
/*--------------------------------------------------------------------------*/
60
 
61
void
62
scope_stack_init PROTO_N ((stack))
63
		 PROTO_T (ScopeStackP stack)
64
{
65
    stack->head = NIL (ScopeStackFrameP);
66
}
67
 
68
void
69
scope_stack_push PROTO_N ((stack, scope))
70
		 PROTO_T (ScopeStackP stack X
71
			  NStringP    scope)
72
{
73
    ScopeStackFrameP frame = ALLOCATE (ScopeStackFrameT);
74
    DStringT         dstring;
75
 
76
    dstring_init (&dstring);
77
    if (frame->head) {
78
	dstring_append_nstring (&dstring, &(stack->head->scope));
79
    }
80
    dstring_append_nstring (&dstring, scope);
81
    dstring_append_cstring (&dstring, "::");
82
    dstring_to_nstring (&dstring, &(frame->scope));
83
    dstring_destroy (&dstring);
84
    frame->next = stack->head;
85
    frame->head = NIL (ScopeMapEntryP);
86
    frame->tail = &(frame->head);
87
    stack->head = frame;
88
}
89
 
90
void
91
scope_stack_pop PROTO_N ((stack))
92
		PROTO_T (ScopeStackP stack)
93
{
94
    ScopeStackFrameP frame = stack->head;
95
    ScopeMapEntryP   entry;
96
    ScopeMapEntryP   next;
97
 
98
    ASSERT (frame);
99
    stack->head = frame->next;
100
    nstring_destroy (&(frame->scope));
101
    for (entry = frame->head; entry; entry = next) {
102
	next = entry->next;
103
	DEALLOCATE (entry);
104
    }
105
    DEALLOCATE (frame);
106
}
107
 
108
EntryP
109
scope_stack_add_rule PROTO_N ((stack, table, key, rule, found_ref))
110
		     PROTO_T (ScopeStackP stack X
111
			      TableP      table X
112
			      NStringP    key X
113
			      RuleP       rule X
114
			      BoolT      *found_ref)
115
{
116
    *found_ref = FALSE;
117
    if (stack->head) {
118
	DStringT dstring;
119
	NStringT nstring;
120
	EntryP   entry;
121
 
122
	dstring_init (&dstring);
123
	dstring_append_nstring (&dstring, &(stack->head->scope));
124
	dstring_append_nstring (&dstring, key);
125
	dstring_to_nstring (&dstring, &nstring);
126
	dstring_destroy (&dstring);
127
	if ((entry = table_get_rule (table, &nstring)) != NIL (EntryP)) {
128
	    *found_ref = TRUE;
129
	    nstring_destroy (&nstring);
130
	    return (entry);
131
	} else if ((entry = table_add_rule (table, &nstring)) !=
132
		   NIL (EntryP)) {
133
	    EntryP         from = table_add_name (table, key);
134
	    ScopeMapEntryP map  = ALLOCATE (ScopeMapEntryT);
135
 
136
	    (void) scope_stack_check_shadowing (stack, from, rule);
137
	    map->next            = NIL (ScopeMapEntryP);
138
	    map->from            = from;
139
	    map->to              = entry;
140
	    *(stack->head->tail) = map;
141
	    stack->head->tail    = &(map->next);
142
	    return (entry);
143
	} else {
144
	    nstring_destroy (&nstring);
145
	    return (NIL (EntryP));
146
	}
147
    } else {
148
	if (table_get_rule (table, key)) {
149
	    *found_ref = TRUE;
150
	}
151
	return (table_add_rule (table, key));
152
    }
153
}
154
 
155
EntryP
156
scope_stack_add_action PROTO_N ((stack, table, key, rule, found_ref))
157
		       PROTO_T (ScopeStackP stack X
158
				TableP      table X
159
				NStringP    key X
160
				RuleP       rule X
161
				BoolT      *found_ref)
162
{
163
    *found_ref = FALSE;
164
    if (stack->head) {
165
	DStringT dstring;
166
	NStringT nstring;
167
	EntryP   entry;
168
 
169
	dstring_init (&dstring);
170
	dstring_append_nstring (&dstring, &(stack->head->scope));
171
	dstring_append_nstring (&dstring, key);
172
	dstring_to_nstring (&dstring, &nstring);
173
	dstring_destroy (&dstring);
174
	if ((entry = table_get_action (table, &nstring)) != NIL (EntryP)) {
175
	    *found_ref = TRUE;
176
	    nstring_destroy (&nstring);
177
	    return (entry);
178
	} else if ((entry = table_add_action (table, &nstring)) !=
179
		   NIL (EntryP)) {
180
	    EntryP         from = table_add_name (table, key);
181
	    ScopeMapEntryP map  = ALLOCATE (ScopeMapEntryT);
182
 
183
	    (void) scope_stack_check_shadowing (stack, from, rule);
184
	    map->next            = NIL (ScopeMapEntryP);
185
	    map->from            = from;
186
	    map->to              = entry;
187
	    *(stack->head->tail) = map;
188
	    stack->head->tail    = &(map->next);
189
	    return (entry);
190
	} else {
191
	    nstring_destroy (&nstring);
192
	    return (NIL (EntryP));
193
	}
194
    } else {
195
	if (table_get_action (table, key)) {
196
	    *found_ref = TRUE;
197
	}
198
	return (table_add_action (table, key));
199
    }
200
}
201
 
202
EntryP
203
scope_stack_add_non_local PROTO_N ((stack, table, key, type, rule))
204
			  PROTO_T (ScopeStackP stack X
205
				   TableP      table X
206
				   NStringP    key X
207
				   EntryP      type X
208
				   RuleP       rule)
209
{
210
    DStringT dstring;
211
    NStringT nstring;
212
    EntryP   entry;
213
 
214
    ASSERT (stack->head);
215
    dstring_init (&dstring);
216
    dstring_append_nstring (&dstring, &(stack->head->scope));
217
    dstring_append_nstring (&dstring, key);
218
    dstring_to_nstring (&dstring, &nstring);
219
    dstring_destroy (&dstring);
220
    if ((entry = table_add_non_local (table, &nstring, type)) !=
221
	NIL (EntryP)) {
222
	EntryP         from = table_add_name (table, key);
223
	ScopeMapEntryP map  = ALLOCATE (ScopeMapEntryT);
224
 
225
	(void) scope_stack_check_shadowing (stack, from, rule);
226
	map->next            = NIL (ScopeMapEntryP);
227
	map->from            = from;
228
	map->to              = entry;
229
	*(stack->head->tail) = map;
230
	stack->head->tail    = &(map->next);
231
	return (entry);
232
    } else {
233
	nstring_destroy (&nstring);
234
	return (NIL (EntryP));
235
    }
236
}
237
 
238
EntryP
239
scope_stack_get_rule PROTO_N ((stack, table, key))
240
		     PROTO_T (ScopeStackP stack X
241
			      TableP      table X
242
			      NStringP    key)
243
{
244
    EntryP entry = table_get_entry (table, key);
245
 
246
    if (entry) {
247
	ScopeStackFrameP frame = stack->head;
248
 
249
	for (; frame; frame = frame->next) {
250
	    ScopeMapEntryP map = frame->head;
251
 
252
	    for (; map; map = map->next) {
253
		if (map->from == entry) {
254
		    if (entry_is_rule (map->to)) {
255
			return (map->to);
256
		    } else {
257
			return (NIL (EntryP));
258
		    }
259
		}
260
	    }
261
	}
262
	if (entry_is_rule (entry)) {
263
	    return (entry);
264
	}
265
    }
266
    return (NIL (EntryP));
267
}
268
 
269
EntryP
270
scope_stack_get_action PROTO_N ((stack, table, key))
271
		       PROTO_T (ScopeStackP stack X
272
				TableP      table X
273
				NStringP    key)
274
{
275
    EntryP entry = table_get_entry (table, key);
276
 
277
    if (entry) {
278
	ScopeStackFrameP frame = stack->head;
279
 
280
	for (; frame; frame = frame->next) {
281
	    ScopeMapEntryP map = frame->head;
282
 
283
	    for (; map; map = map->next) {
284
		if (map->from == entry) {
285
		    if (entry_is_action (map->to)) {
286
			return (map->to);
287
		    } else {
288
			return (NIL (EntryP));
289
		    }
290
		}
291
	    }
292
	}
293
	if (entry_is_action (entry)) {
294
	    return (entry);
295
	}
296
    }
297
    return (NIL (EntryP));
298
}
299
 
300
EntryP
301
scope_stack_get_non_local PROTO_N ((stack, table, key, scope))
302
			  PROTO_T (ScopeStackP stack X
303
				   TableP      table X
304
				   NStringP    key X
305
				   NStringP    scope)
306
{
307
    EntryP entry = table_get_entry (table, key);
308
 
309
    if (entry) {
310
	ScopeStackFrameP frame = stack->head;
311
 
312
	for (; frame; frame = frame->next) {
313
	    ScopeMapEntryP map = frame->head;
314
 
315
	    for (; map; map = map->next) {
316
		if (map->from == entry) {
317
		    if (entry_is_non_local (map->to)) {
318
			nstring_copy (scope, &(frame->scope));
319
			return (map->to);
320
		    } else {
321
			return (NIL (EntryP));
322
		    }
323
		}
324
	    }
325
	}
326
    }
327
    return (NIL (EntryP));
328
}
329
 
330
BoolT
331
scope_stack_check_shadowing PROTO_N ((stack, from, rule))
332
			    PROTO_T (ScopeStackP stack X
333
				     EntryP      from X
334
				     RuleP       rule)
335
{
336
    ScopeStackFrameP frame = stack->head;
337
 
338
    for (; frame; frame = frame->next) {
339
	ScopeMapEntryP entry;
340
 
341
	for (entry = frame->head; entry; entry = entry->next) {
342
	    if (entry->from == from) {
343
		E_shadows_non_local (entry_key (from), entry_key (entry->to),
344
				     rule);
345
		return (TRUE);
346
	    }
347
	}
348
    }
349
    if (entry_is_rule (from) || entry_is_action (from) ||
350
	entry_is_basic (from)) {
351
	E_shadows_global (entry_key (from), rule);
352
	return (TRUE);
353
    }
354
    return (FALSE);
355
}
356
 
357
/*
358
 * Local variables(smf):
359
 * eval: (include::add-path-entry "../os-interface" "../library")
360
 * eval: (include::add-path-entry "../generated")
361
 * end:
362
**/