summaryrefslogtreecommitdiff
path: root/bcc/misc/test/extern.t
blob: dac5f5c6d59e80dfb49598be8cce72344900cc0f (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* extern.c - external declarations (K & R p218-9) */

#include "def.h"
#include "globvar.h"
#include "symbol.h"

struct typestruct *declarator();
struct typestruct *extypespec();
int paramlist();
struct typestruct *typespec();

/*=============================================================================
	-- notation is 1st attempt at EBNF (see Modula-2 book)
	program = { type-definition | function-or-data-definition }.
	type-definition = "typedef" type-definition-list.
=============================================================================*/

program()
{
	nextsym();
	while ( sym != EOFSYM )
	{
		if ( sym = TYPEDEFDECL )
			deftypes();
		else
			functordata();
	}
}

/*=============================================================================
	type-definition-list. -- not implemented
=============================================================================*/

deftypes()
{}

/*=============================================================================
	function-or-data-definition = external-type-specification
								  ( function-definition | init-declarators ).
	function-definition = "(" parameter-list ")" arg-list compound-statement.
	-- this is different from K & R who omit typedefs and static functions
=============================================================================*/

functordata()
{
	struct typestruct *type;
	int nargs;

	type = extypespec();
	if ( sym == LPAREN )
	{
		nextsym();
		nargs = paramlist();
		need( RPAREN );
		arglist( nargs );
		need( LBRACE );
		compound( 1 + locptr-pstartloc );/* 1 = function flag, rest = nargs */
		oldlevel();
	}
	else
		initdecl( type );
}

/*=============================================================================
	external-type-specification = extern-sc type-specifier declarator.
	extern-sc = [ extern | static ].
=============================================================================*/

struct typestruct *extypespec()
{
	int sc;

	switch( sym )
	{
	case EXTERNDECL:
	case STATICDECL:
		sc = sym; nextsym(); break;
	default:
		sc = EXTERNDECL; break;
	}
	return declarator( typespec() );
}

/*=============================================================================
	parameter-list = [identifier { "," identifier }].
=============================================================================*/

int paramlist()
{
	int nargs;

	locptr = pstartloc;
	newlevel();
	nargs = 0;
	while ( sym == IDENT )
	{
		addloc( gsname, itype, 0 );	/* fix up types and offsets later */
		nextsym();
		++nargs;
		if ( sym != COMMA )
			break;
		nextsym();
	}
	return nargs;
}

/*=============================================================================
	arg-list =
=============================================================================*/

arglist( nargs )
int nargs;
{
	struct symstruct *symptr;
	int argsize, argsp;
	int lastsym;
	struct typestruct *basetype;
	char declflag;

	declflag = TRUE;
	do
	{
		switch( sym )
		{
		case TYPEDECL:
			basetype = gsymptr->type;
			nextsym();
			getarg1( basetype );
			break;
		case STRUCTDECL:
		case UNIONDECL:
			lastsym = sym;
			nextsym();
			getarg( declsu( lastsym ) );
			break;
		case AUTODECL:
		case EXTERNDECL:
		case REGDECL:
		case STATICDECL:
		case SEMICOLON:
			nextsym();
			break;
		default:
			declflag = FALSE;
			break;
		}
	}
	while ( declflag );
	argsp = -2;
	symptr = pstartloc;
	while ( nargs && symptr < locptr )	/* convert arg sizes to offsets */
	{
		argsize = symptr->offset;
		if ( symptr->type == ctype )
		{
			++argsp;
			--argsize;
		}
		symptr->offset = argsp;
		argsp += argsize;
		if ( symptr == pstartloc )
			argsp += 2;
		--nargs;
		++symptr;
	}
	if ( nargs )
		error( "arguments not all declared" );
}
	
/* getarg( basetype ) - fill in argument types and sizes */

getarg1( basetype )
struct typestruct *basetype;
{
	char sname[NAMESIZE];
	struct typestruct *type;
	int size;
	struct symstruct *symptr;

	if ( sym != SEMICOLON )
	while ( TRUE )
	{
		type = basetype;
		size = getvar( sname, &type );
		if ( (symptr = findlorg( sname )) == NULL || symptr->level != ARGLEVEL )
			error( "variable not in argument list" );
		else if ( symptr->offset != 0 ) /* already in arg list */
			multidecl( sname, type, symptr );
		else
		{
			if ( size < 2 || type->typeop == ATYPEOP )
				size = 2;
			symptr->offset = size;
			symptr->type = type;
		}
		if ( sym == COMMA )
			nextsym();
		else
			break;
	}
	ns();
}

struct typestruct *declarator( basetype )
struct typestruct *basetype;
{
	char sname[NAMESIZE];
	int size;
	struct symstruct *multi;

	size = getvar( sname, &type );
	if ( multi = findlorg( sname ) )
		multidecl( sname, type, multi );
	else	
		addglb( sname, type, size );
	nextsym();
}

struct typestruct *typespec()
{
	int lastsym;

	switch( sym )
	{
	case TYPEDECL:
		nextsym();
		return gsymptr->type;
	case STRUCTDECL:
	case UNIONDECL:
		lastsym = sym;
		nextsym();
		return declsu( lastsym );
	default:
		return itype;
	}
}