|
|
1.1 root 1: /*ident "@(#)ctrans:src/norm2.c 1.1.5.12" */
2: /************************************************************************
3:
4: C++ source for cfront, the C++ compiler front-end
5: written in the computer science research center of Bell Labs
6:
7: Copyright (c) 1984 AT&T, Inc. All rigths Reserved
8: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
9:
10: norm2.c:
11:
12: "normalization" handles problems which could have been handled
13: by the syntax analyser; but has not been done. The idea is
14: to simplify the grammar and the actions associated with it,
15: and to get a more robust error handling
16:
17: ****************************************************************************/
18:
19: #include "cfront.h"
20: #include "size.h"
21: const NBITE = (CHUNK-8)/sizeof(name)-1;
22: const EBITE = (CHUNK-8)/sizeof(expr)-1;
23: const SBITE = (CHUNK-8)/sizeof(stmt)-1;
24:
25: #ifdef DBG
26: long node_id = 0;
27: #define DBCHECK() if(node::allocated) error('i',"allocated node (id %d, base%k) on free list! (src: \"%s\", %d",node::id,node::base,__FILE__,__LINE__);
28: #else
29: #define DBCHECK() /**/
30: #endif
31:
32: fct::fct(Ptype t, Pname arg, TOK known)
33: {
34: base = FCT;
35: nargs_known = known;
36: returns = t;
37: argtype = arg;
38: DBID();
39:
40: if (arg==0 || arg->base==ELIST) return;
41: //error('d',"fct::fct %d sig %d",this,f_signature);
42: register Pname n;
43: Pname pn = 0;
44: for (n=arg; n; pn=n,n=n->n_list) {
45: if( n->n_sto==EXTERN ) error("externA");
46: if( n->n_sto==STATIC ) error("static used forA%n",arg);
47:
48: switch (n->tp->base) {
49: case VOID:
50: argtype = 0;
51: nargs_known = 1;
52: if(n->n_initializer)
53: error("voidFA");
54: else if (n->string)
55: error("voidFA%n",n);
56: else if (nargs || n->n_list) {
57: error("voidFA");
58: nargs_known = 0;
59: }
60: nargs = 0;
61: break;
62: case CLASS:
63: case ENUM:
64: error("%k defined inAL (will not be in scope at point of call)",n->tp->base);
65: if (n == argtype)
66: argtype = n->n_list;
67: else
68: pn->n_list = n->n_list;
69: break;
70: default:
71: nargs++;
72: }
73: }
74: }
75:
76: Pexpr expr_free;
77:
78: expr::expr(TOK ba, Pexpr a, Pexpr b)
79: {
80: register Pexpr p;
81:
82: if (this) goto ret;
83:
84: if ( (p=expr_free) == 0 ) {
85: register Pexpr q = (Pexpr) chunk(1);
86: for (p=expr_free=&q[EBITE-1]; q<p; p--) {
87: p->e1 = p-1;
88: DB(p->node::allocated=0);
89: }
90: (p+1)->e1 = 0;
91: DB(p->node::allocated=0);
92: }
93: else
94: expr_free = p->e1;
95:
96: this = p;
97: DBCHECK();
98:
99: permanent = 0;
100: tp = 0;
101: tp2 = 0;
102: ret:
103: base = ba;
104: e1 = a;
105: e2 = b;
106: DBID();
107: }
108:
109: expr::~expr()
110: {
111: DB( if(!node::allocated) error('i',"deleting unallocated expr:%k! -- id==%d",base,node::id);
112: node::allocated = 0;
113: );
114: e1 = expr_free;
115: expr_free = this;
116: this = 0;
117: }
118:
119: Pstmt stmt_free;
120:
121: stmt::stmt(TOK ba, loc ll, Pstmt a)
122: {
123: register Pstmt p;
124:
125: if ( (p=stmt_free) == 0 ) {
126: register Pstmt q = (Pstmt) chunk(1);
127: for (p=stmt_free=&q[SBITE-1]; q<p; p--) {
128: p->s_list = p-1;
129: DB(p->node::allocated=0);
130: }
131: (p+1)->s_list = 0;
132: DB(p->node::allocated=0);
133: }
134: else
135: stmt_free = p->s_list;
136:
137: this = p;
138: DBCHECK();
139:
140: permanent = 0;
141: e = e2 = 0;
142: memtbl = 0;
143: else_stmt = 0;
144: s_list = 0;
145:
146: // Ns++;
147: base = ba;
148: where = ll;
149: s=a;
150: DBID();
151: }
152:
153: stmt::~stmt()
154: {
155: DB( if(!node::allocated) error('i',"deleting unallocated stmt:%k! -- id==%d",base,node::id);
156: node::allocated = 0;
157: );
158: // NFs++;
159: s_list = stmt_free;
160: stmt_free = this;
161: this = 0;
162: }
163:
164: classdef::classdef(TOK b)
165: {
166: base = CLASS;
167: csu = b;
168: memtbl = new table(CTBLSIZE,0,0);
169: c_funqf = 0;
170: c_funqr = 0;
171: DBID();
172: }
173:
174: basetype::basetype(TOK b, Pname n)
175: {
176: // Nbt++;
177: switch (b) {
178: case 0: break;
179: case TYPEDEF: b_typedef = 1; break;
180: case INLINE: b_inline = 1; break;
181: case VIRTUAL: b_virtual = 1; break;
182: case CONST: b_const = 1; break;
183: case UNSIGNED: b_unsigned = 1; break;
184: case FRIEND:
185: case OVERLOAD:
186: case EXTERN:
187: case STATIC:
188: case AUTO:
189: case REGISTER: b_sto = b; break;
190: case SHORT: b_short = 1; break;
191: case LONG: b_long = 1; break;
192: case ANY:
193: case ZTYPE:
194: case VOID:
195: case CHAR:
196: case INT:
197: case FLOAT:
198: case LDOUBLE:
199: case DOUBLE: base = b; break;
200: case TYPE:
201: case COBJ:
202: case EOBJ:
203: case FIELD:
204: case ASM:
205: base = b;
206: b_name = n;
207: break;
208: case SIGNED:
209: case VOLATILE:
210: error('w',"\"%k\" not implemented (ignored)",b);
211: break;
212: default:
213: error('i',"badBT:%k",b);
214: }
215: DBID();
216: }
217:
218: Pname name_free;
219:
220: #ifdef __cplusplus
221: name::name(char* s) : expr(NAME,0,0)
222: #else
223: name::name(char* s) : (NAME,0,0)
224: #endif
225: {
226: register Pname p;
227:
228: if ( (p=name_free) == 0 ) {
229: register Pname q = (Pname) chunk(1);
230: for (p=name_free=&q[NBITE-1]; q<p; p--) {
231: p->n_tbl_list = p-1;
232: DB(p->node::allocated=0);
233: }
234: (p+1)->n_tbl_list = 0;
235: DB(p->node::allocated=0);
236: }
237: else
238: name_free = p->n_tbl_list;
239:
240: this = p;
241: // DBCHECK() called in expr::expr
242:
243: string = s;
244: where = curloc;
245: lex_level = bl_level;
246:
247: // beware of alignment differences & pointer-zeros that is not int-zeros
248: tp = 0;
249: n_initializer = 0;
250: n_table = 0;
251: n_oper = 0;
252: n_sto = 0;
253: n_stclass = 0;
254: n_scope = 0;
255: n_union = 0;
256: n_evaluated = 0;
257: n_xref = 0;
258: n_protect = 0;
259: n_dcl_printed = 0;
260: n_addr_taken = 0;
261: n_used = 0;
262: n_assigned_to = 0;
263: n_val = 0;
264: n_offset = 0;
265: n_list = 0;
266: n_tbl_list = 0;
267: n_qualifier = 0;
268: n_key = 0;
269: n_anon = 0;
270: }
271:
272: name::~name()
273: {
274: DB( if(!node::allocated) error('i',"deleting unallocated name %s! -- id==%d",string?string:"???",node::id);
275: node::allocated = 0;
276: );
277: n_tbl_list = name_free;
278: name_free = this;
279: this = 0;
280: }
281:
282:
283: nlist::nlist(Pname n)
284: {
285: head = n;
286: for (Pname nn=n; nn->n_list; nn=nn->n_list);
287: tail = nn;
288: }
289:
290: void nlist::add_list(Pname n)
291: {
292: if (n->tp && (n->tp->defined & IN_ERROR)) return;
293:
294: tail->n_list = n;
295: for (Pname nn=n; nn->n_list; nn=nn->n_list);
296: tail = nn;
297: }
298:
299: Pname name_unlist(Pnlist l)
300: {
301: if (l == 0) return 0;
302: Pname n = l->head;
303:
304: delete l;
305: return n;
306: }
307:
308: Pstmt stmt_unlist(Pslist l)
309: {
310: if (l == 0) return 0;
311: Pstmt s = l->head;
312: // NFl++;
313:
314: delete l;
315: return s;
316: }
317:
318: Pexpr expr_unlist(Pelist l)
319: {
320: if (l == 0) return 0;
321: Pexpr e = l->head;
322: // NFl++;
323:
324: delete l;
325: return e;
326: }
327:
328: void sig_name(Pname n)
329: {
330: static char buf[256];
331: buf[0] = '_';
332: buf[1] = '_';
333: buf[2] = 'o';
334: buf[3] = 'p';
335: char* p = n->tp->signature(buf+4);
336: if (255 < p-buf) error('i',"sig_name():N buffer overflow");
337: char *s = new char [ p - buf + 1 ];
338: strcpy(s,buf);
339: n->string = s;
340: n->tp = 0;
341: }
342:
343: Ptype tok_to_type(TOK b)
344: {
345: Ptype t;
346: switch (b) {
347: case CHAR: t = char_type; break;
348: case SHORT: t = short_type; break;
349: case LONG: t = long_type; break;
350: case UNSIGNED: t = uint_type; break;
351: case FLOAT: t = float_type; break;
352: case DOUBLE: t = double_type; break;
353: case LDOUBLE: t = ldouble_type; break;
354: case VOID: t = void_type; break;
355: default: error("illegalK:%k",b);
356: case INT: t = int_type;
357: }
358: return t;
359: }
360:
361: Pbase defa_type;
362: Pbase moe_type;
363: Pexpr dummy;
364: Pexpr zero;
365:
366: Pclass ccl;
367: Plist modified_tn = 0;
368:
369: Plist local_tn = 0;
370: Plist local_blk = 0;
371: Plist local_class = 0;
372:
373: Plist nested_tn = 0;
374: Plist nested_type = 0;
375:
376: void memptrdcl(Pname bn, Pname tn, Ptype ft, Pname n)
377: {
378: Pptr p = new ptr(PTR,0);
379: p->memof = Pclass(Pbase(bn->tp)->b_name->tp);
380: Pbase b = new basetype(TYPE,tn);
381: PERM(p);
382: Pfct f = Pfct(ft);
383: Ptype t = n->tp;
384: if (t) {
385: p->typ = t;
386: ltlt:
387: switch (t->base) {
388: case PTR:
389: case RPTR:
390: case VEC:
391: if (Pptr(t)->typ == 0) {
392: Pptr(t)->typ = b;
393: break;
394: }
395: t = Pptr(t)->typ;
396: goto ltlt;
397: default:
398: error('s',"P toMFT too complicated");
399: }
400: }
401: else
402: p->typ = b;
403: f->returns = p;
404: n->tp = f;
405: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.