1 /* D E C L A R A T I O N S */
24 #define PC_BUFSIZ (sizeof(struct file) - (int)((struct file *)0)->bufadr)
26 int proclevel = 0; /* nesting level of procedures */
27 int parlevel = 0; /* nesting level of parametersections */
28 int expect_label = 0; /* so the parser knows that we expect a label */
29 static int in_type_defs; /* in type definition part or not */
32 /* ISO section 6.2.1, p. 93 */
33 Block(struct def *df;)
37 { text_label = (label) 0; }
43 if( df ) EndBlock(df);
44 FreeNode(BlockScope->sc_lablist);
56 nd->nd_next = CurrentScope->sc_lablist;
57 CurrentScope->sc_lablist = nd;
64 nd->nd_next = CurrentScope->sc_lablist;
65 CurrentScope->sc_lablist = nd;
73 Module(struct def *df; arith *i;)
77 ConstantDefinitionPart
81 /* resolve forward references */
84 VariableDeclarationPart
90 save_label = text_label;
92 ProcedureAndFunctionDeclarationPart
93 { text_label = save_label;
98 /* needed with labeldefinitions
101 BlockScope = CurrentScope;
104 *i = CodeBeginBlock( df );
111 ConstantDefinitionPart:
115 ConstantDefinition ';'
129 VariableDeclarationPart:
133 VariableDeclaration ';'
138 ProcedureAndFunctionDeclarationPart:
148 /* ISO section 6.1.6, p. 92 */
149 Label(struct node **pnd;)
152 extern char *sprint();
153 } : { expect_label = 1; }
154 INTEGER /* not really an integer, in [0..9999] */
155 { if( dot.TOK_INT < 0 || dot.TOK_INT > 9999 ) {
156 if( dot.TOK_INT != -1 ) /* This means insertion */
157 error("label must lie in closed interval [0..9999]");
161 sprint(lab, "%d", (int) dot.TOK_INT);
162 *pnd = MkLeaf(Name, &dot);
163 (*pnd)->nd_IDF = str2idf(lab, 1);
170 /* ISO section 6.3, p. 95 */
173 register struct idf *id;
174 register struct def *df;
177 IDENT { id = dot.TOK_IDF; }
179 { if( df = define(id,CurrentScope,D_CONST) ) {
181 df->df_type = nd->nd_type;
182 df->df_flags |= D_SET;
184 if (options['g']) stb_string(df, D_CONST);
185 #endif /* DBSYMTAB */
190 /* ISO section 6.4.1, p. 96 */
193 register struct idf *id;
194 register struct def *df;
197 IDENT { id = dot.TOK_IDF; }
199 { if( df = define(id, CurrentScope, D_TYPE) ) {
201 df->df_flags |= D_SET;
203 if (options['g']) stb_string(df, D_TYPE);
204 #endif /* DBSYMTAB */
209 TypeDenoter(register struct type **ptp;):
210 /* This is a changed rule, because the grammar as specified in the
211 * reference is not LL(1), and this gives conflicts.
213 TypeIdentifierOrSubrangeType(ptp)
222 TypeIdentifierOrSubrangeType(register struct type **ptp;)
224 struct node *nd1, *nd2;
226 /* This is a new rule because the grammar specified by the standard
227 * is not exactly LL(1) (see TypeDenoter).
231 IDENT { nd1 = MkLeaf(Name, &dot); }
234 /* at this point IDENT must be a TypeIdentifier !! */
235 { chk_type_id(ptp, nd1);
239 /* at this point IDENT must be a Constant !! */
240 { (void) ChkConstant(nd1); }
242 { *ptp = subr_type(nd1, nd2);
248 Constant(&nd1) UPTO Constant(&nd2)
249 { *ptp = subr_type(nd1, nd2);
256 TypeIdentifier(register struct type **ptp;):
257 IDENT { register struct node *nd = MkLeaf(Name, &dot);
258 chk_type_id(ptp, nd);
263 /* ISO section 6.5.1, p. 105 */
266 struct node *VarList;
269 IdentifierList(&VarList) ':' TypeDenoter(&tp)
270 { EnterVarList(VarList, tp, proclevel > 0); }
273 /* ISO section 6.6.1, p. 108 */
278 register struct scopelist *scl;
279 register struct def *df;
281 /* This is a changed rule, because the grammar as specified in the
282 * reference is not LL(1), and this gives conflicts.
284 * ProcedureHeading without a FormalParameterList can be a
285 * ProcedureIdentification, i.e. the IDENT used in the Heading is
286 * also used in a "forward" declaration.
289 ProcedureHeading(&nd, &tp) ';'
290 { scl = CurrVis; close_scope(0); }
293 { DoDirective(dot.TOK_IDF, nd, tp, scl, 0); }
295 { df = DeclProc(nd, tp, scl);
297 if (options['g']) stb_string(df, D_PROCEDURE);
298 #endif /* DBSYMTAB */
301 { /* open_scope() is simulated in DeclProc() */
303 if (options['g']) stb_string(df, D_PEND);
304 #endif /* DBSYMTAB */
310 ProcedureHeading(register struct node **pnd; register struct type **ptp;)
316 *pnd = MkLeaf(Name, &dot);
319 FormalParameterList(&fpl)
321 struct paramlist *pr = 0;
324 /* procedure declaration */
325 nb_pars = EnterParamList(fpl, &pr);
327 /* procedure parameter */
328 nb_pars = EnterParTypes(fpl, &pr);
330 *ptp = proc_type(pr, nb_pars);
336 proc_type((struct paramlist *)0,
337 (proclevel > 1) ? pointer_size : (arith) 0);
343 /* see also Functiondeclaration (6.6.2, p. 110)
344 * Not actually an identifier but 'letter {letter | digit}'
349 /* ISO section 6.6.1, p. 108 */
354 register struct scopelist *scl;
355 register struct def *df;
357 /* This is a changed rule, because the grammar as specified in the
358 * reference is not LL(1), and this gives conflicts.
361 FunctionHeading(&nd, &tp) ';'
362 { scl = CurrVis; close_scope(0); }
367 "function \"%s\": illegal declaration",
368 nd->nd_IDF->id_text);
370 else DoDirective(dot.TOK_IDF, nd, tp, scl, 1);
373 { if( df = DeclFunc(nd, tp, scl) ) {
375 - ResultType(df->df_type)->tp_size;
377 CurrentScope->sc_off =
378 df->prc_res - int_size;
380 if (options['g']) stb_string(df, D_FUNCTION);
381 #endif /* DBSYMTAB */
387 if (options['g']) stb_string(df, D_PEND);
388 #endif /* DBSYMTAB */
392 /* open_scope() is simulated in DeclFunc() */
398 FunctionHeading(register struct node **pnd; register struct type **ptp;)
400 /* This is the Function AND FunctionIdentification part.
401 If it is a identification, *ptp is set to NULLTYPE.
403 struct node *fpl = NULLNODE;
405 struct paramlist *pr = 0;
406 arith nb_pars = (proclevel > 1) ? pointer_size : 0;
409 IDENT { *pnd = MkLeaf(Name, &dot);
414 FormalParameterList(&fpl)
416 /* function declaration */
417 nb_pars = EnterParamList(fpl, &pr);
419 /* function parameter */
420 nb_pars = EnterParTypes(fpl, &pr);
425 ':' TypeIdentifier(&tp)
426 { if( IsConstructed(tp) ) {
428 "function has an illegal result type");
431 *ptp = func_type(pr, nb_pars, tp);
437 /* ISO section 6.4.2.1, p. 96 */
438 OrdinalType(register struct type **ptp;):
439 /* This is a changed rule, because the grammar as specified in the
440 * reference states that a SubrangeType can start with an IDENT and
441 * so can an OrdinalTypeIdentifier, and this is not LL(1).
443 TypeIdentifierOrSubrangeType(ptp)
448 /* ISO section 6.4.2.3, p. 97 */
449 EnumeratedType(register struct type **ptp;)
451 struct node *EnumList;
454 '(' IdentifierList(&EnumList) ')'
455 { register struct type *tp =
456 standard_type(T_ENUMERATION, word_align, word_size);
459 EnterEnumList(EnumList, tp);
460 if( tp->enm_ncst == 0 )
463 if( ufit(tp->enm_ncst-1, i) ) {
469 } while( i < word_size );
473 IdentifierList(register struct node **nd;)
475 register struct node *tnd;
477 IDENT { *nd = tnd = MkLeaf(Name, &dot); }
480 { tnd->nd_next = MkLeaf(Name, &dot);
486 /* ISO section 6.4.3.2, p. 98 */
487 StructuredType(register struct type **ptp;)
489 unsigned short packed = 0;
492 PACKED { packed = T_PACKED; }
494 UnpackedStructuredType(ptp, packed)
497 UnpackedStructuredType(register struct type **ptp; unsigned short packed;):
498 ArrayType(ptp, packed)
500 RecordType(ptp, packed)
507 /* ISO section 6.4.3.2, p. 98 */
508 ArrayType(register struct type **ptp; unsigned short packed;)
511 register struct type *tp2;
516 { *ptp = tp2 = construct_type(T_ARRAY, tp);
517 tp2->tp_flags |= packed;
521 { tp2->arr_elem = construct_type(T_ARRAY, tp);
523 tp2->tp_flags |= packed;
527 OF ComponentType(&tp)
528 { tp2->arr_elem = tp;
530 if( tp->tp_flags & T_HASFILE )
531 (*ptp)->tp_flags |= T_HASFILE;
535 Indextype(register struct type **ptp;):
539 ComponentType(register struct type **ptp;):
543 /* ISO section 6.4.3.3, p. 99 */
544 RecordType(register struct type **ptp; unsigned short packed;)
546 register struct scope *scope;
547 register struct def *df;
548 struct selector *sel = 0;
550 int xalign = struct_align;
553 { open_scope(); /* scope for fields of record */
554 scope = CurrentScope;
557 FieldList(scope, &size, &xalign, packed, &sel)
559 warning("empty record declaration");
562 *ptp = standard_type(T_RECORD, xalign, size);
563 (*ptp)->rec_scope = scope;
564 (*ptp)->rec_sel = sel;
565 (*ptp)->tp_flags |= packed;
567 /* copy the file component flag */
569 while( df && !(df->df_type->tp_flags & T_HASFILE) )
570 df = df->df_nextinscope;
573 (*ptp)->tp_flags |= T_HASFILE;
578 FieldList(struct scope *scope; arith *cnt; int *palign; unsigned short packed;
579 struct selector **sel;):
580 /* This is a changed rule, because the grammar as specified in the
581 * reference is not LL(1), and this gives conflicts.
582 * Those irritating, annoying (Siklossy !!) semicolons.
587 FixedPart(scope, cnt, palign, packed, sel)
589 VariantPart(scope, cnt, palign, packed, sel)
592 FixedPart(struct scope *scope; arith *cnt; int *palign; unsigned short packed;
593 struct selector **sel;):
594 /* This is a changed rule, because the grammar as specified in the
595 * reference is not LL(1), and this gives conflicts.
596 * Again those frustrating semicolons !!
598 RecordSection(scope, cnt, palign, packed)
599 FixedPartTail(scope, cnt, palign, packed, sel)
602 FixedPartTail(struct scope *scope; arith *cnt; int *palign;
603 unsigned short packed; struct selector **sel;):
604 /* This is a new rule because the grammar specified by the standard
605 * is not exactly LL(1).
606 * We see the light at the end of the tunnel !
616 VariantPart(scope, cnt, palign, packed, sel)
618 RecordSection(scope, cnt, palign, packed)
619 FixedPartTail(scope, cnt, palign, packed, sel)
623 RecordSection(struct scope *scope; arith *cnt; int *palign;
624 unsigned short packed;)
626 struct node *FldList;
630 IdentifierList(&FldList) ':' TypeDenoter(&tp)
632 lcm(*palign, packed ? tp->tp_palign : word_align);
633 EnterFieldList(FldList, tp, scope, cnt, packed);
637 VariantPart(struct scope *scope; arith *cnt; int *palign;
638 unsigned short packed; struct selector **sel;)
644 register arith ncst = 0;/* the number of values of the tagtype */
645 register struct selector **sp;
646 extern char *Malloc();
648 /* This is a changed rule, because the grammar as specified in the
649 * reference is not LL(1), and this gives conflicts.
650 * We're almost there !!
653 { *sel = (struct selector *) Malloc(sizeof(struct selector));
654 (*sel)->sel_ptrs = 0;
657 VariantSelector(&tp, &id)
659 df = define(id, scope, D_FIELD);
660 /* ISO 6.4.3.3 (p. 100)
661 * The standard permits the integertype as tagtype, but demands that the set
662 * of values denoted by the case-constants is equal to the set of values
663 * specified by the tagtype.
665 if( !(tp->tp_fund & T_INDEX)) {
666 error("illegal type in variant");
672 getbounds(tp, &lb, &ub);
674 if (ncst < 0 || ncst > (~(1L<<(8*sizeof(arith)-1)))/sizeof(struct selector *)) {
675 error("range of variant tag too wide");
679 /* initialize selector */
680 (*sel)->sel_ptrs = (struct selector **)
681 Malloc((unsigned)ncst * sizeof(struct selector *));
682 (*sel)->sel_ncst = ncst;
685 /* initialize tagvalue-table */
686 sp = (*sel)->sel_ptrs;
687 while( ncst-- ) *sp++ = *sel;
690 (*sel)->sel_type = tp;
694 packed ? (F_PACKED | F_SELECTOR) : F_SELECTOR;
695 df->fld_off = align(*cnt,
696 packed ? tp->tp_palign : tp->tp_align);
698 (packed ? tp->tp_psize : tp->tp_size);
703 Variant(scope, &tcnt, palign, packed, *sel)
705 VariantTail(scope, &tcnt, &max, cnt, palign, packed, *sel)
707 if( sp = (*sel)->sel_ptrs ) {
710 ncst = (*sel)->sel_ncst;
718 error("record variant part: each tagvalue must have a variant");
723 VariantTail(register struct scope *scope; arith *tcnt; arith *max; arith *cnt;
724 int *palign; unsigned short packed; struct selector *sel;):
725 /* This is a new rule because the grammar specified by the standard
726 * is not exactly LL(1).
727 * At last, the garden of Eden !!
738 Variant(scope, tcnt, palign, packed, sel)
739 { if( *tcnt > *max ) *max = *tcnt; }
740 VariantTail(scope, tcnt, max, cnt, palign, packed, sel)
744 VariantSelector(register struct type **ptp; register struct idf **pid;)
746 register struct node *nd;
748 /* This is a changed rule, because the grammar as specified in the
749 * reference is not LL(1), and this gives conflicts.
752 IDENT { nd = MkLeaf(Name, &dot); }
754 /* Old fashioned ! at this point the IDENT represents
757 { warning("old-fashioned syntax ':' missing");
758 chk_type_id(ptp, nd);
762 /* IDENT is now the TagField */
771 Variant(struct scope *scope; arith *cnt; int *palign; unsigned short packed;
772 struct selector *sel;)
775 struct selector *sel1 = 0;
777 CaseConstantList(&nd)
779 '(' FieldList(scope, cnt, palign, packed, &sel1) ')'
780 { TstCaseConstants(nd, sel, sel1);
785 CaseConstantList(struct node **nd;)
789 Constant(&nd1) { *nd = nd1; }
791 ',' Constant(&(nd1->nd_next))
792 { nd1 = nd1->nd_next; }
796 /* ISO section 6.4.3.4, p. 101 */
797 SetType(register struct type **ptp; unsigned short packed;):
798 SET OF OrdinalType(ptp)
799 { *ptp = set_type(*ptp, packed); }
802 /* ISO section 6.4.3.5, p. 101 */
803 FileType(register struct type **ptp;):
805 { *ptp = construct_type(T_FILE, NULLTYPE);
806 (*ptp)->tp_flags |= T_HASFILE;
808 ComponentType(&(*ptp)->next)
809 { if( (*ptp)->next->tp_flags & T_HASFILE ) {
810 error("file type has an illegal component type");
811 (*ptp)->next = error_type;
814 if( (*ptp)->next->tp_size > PC_BUFSIZ )
815 (*ptp)->tp_size = (*ptp)->tp_psize =
816 (*ptp)->next->tp_size +
817 sizeof(struct file) - PC_BUFSIZ;
822 /* ISO section 6.4.4, p. 103 */
823 PointerType(register struct type **ptp;)
825 register struct node *nd;
826 register struct def *df;
829 { *ptp = construct_type(T_POINTER, NULLTYPE); }
831 { nd = MkLeaf(Name, &dot);
832 df = lookup(nd->nd_IDF, CurrentScope, D_INUSE);
833 /* if( !df && CurrentScope == GlobalScope)
834 df = lookup(nd->nd_IDF, PervasiveScope, D_INUSE);
837 (!df || (df->df_kind & (D_ERROR | D_FORWTYPE)))
839 /* forward declarations only in typedefintion
844 chk_type_id(&(*ptp)->next, nd);
850 /* ISO section 6.6.3.1, p. 112 */
851 FormalParameterList(struct node **pnd;)
856 { *pnd = nd = MkLeaf(Link, &dot); }
857 FormalParameterSection(nd)
859 { nd->nd_right = MkLeaf(Link, &dot);
862 ';' FormalParameterSection(nd)
867 FormalParameterSection(struct node *nd;):
868 /* This is a changed rule, because the grammar as specified
869 * in the reference is not LL(1), and this gives conflicts.
871 { /* kind of parameter */
876 /* ValueParameterSpecification */
878 { nd->nd_INT = (D_VALPAR | D_SET); }
880 /* VariableParameterSpecification */
882 { nd->nd_INT = (D_VARPAR | D_USED); }
884 IdentifierList(&(nd->nd_left)) ':'
886 /* ISO section 6.6.3.7.1, p. 115 */
887 /* ConformantArrayParameterSpecification */
888 ConformantArraySchema(&(nd->nd_type))
890 TypeIdentifier(&(nd->nd_type))
892 { if( nd->nd_type->tp_flags & T_HASFILE &&
893 (nd->nd_INT & D_VALPAR) ) {
894 error("value parameter can't have a filecomponent");
895 nd->nd_type = error_type;
899 ProceduralParameterSpecification(&(nd->nd_left), &(nd->nd_type))
900 { nd->nd_INT = (D_VALPAR | D_SET); }
902 FunctionalParameterSpecification(&(nd->nd_left), &(nd->nd_type))
903 { nd->nd_INT = (D_VALPAR | D_SET); }
907 ProceduralParameterSpecification(register struct node **pnd;
908 register struct type **ptp;):
910 ProcedureHeading(pnd, ptp)
914 FunctionalParameterSpecification(register struct node **pnd;
915 register struct type **ptp;):
917 FunctionHeading(pnd, ptp)
921 "illegal function parameter declaration");
927 ConformantArraySchema(register struct type **ptp;):
928 PackedConformantArraySchema(ptp)
931 UnpackedConformantArraySchema(ptp)
934 PackedConformantArraySchema(register struct type **ptp;)
939 { tp = construct_type(T_ARRAY, NULLTYPE);
940 tp->tp_flags |= T_PACKED;
943 Index_TypeSpecification(ptp, tp)
946 OF TypeIdentifier(ptp)
947 { if( (*ptp)->tp_flags & T_HASFILE )
948 tp->tp_flags |= T_HASFILE;
954 UnpackedConformantArraySchema(register struct type **ptp;)
956 struct type *tp, *tp2;
959 { *ptp = tp = construct_type(T_ARRAY,NULLTYPE);}
961 Index_TypeSpecification(&tp2, tp)
965 construct_type(T_ARRAY, NULLTYPE);
968 ';' Index_TypeSpecification(&tp2, tp)
976 ConformantArraySchema(&tp2)
978 { if( tp2->tp_flags & T_HASFILE )
979 (*ptp)->tp_flags |= T_HASFILE;
984 Index_TypeSpecification(register struct type **ptp; register struct type *tp;)
986 register struct def *df1, *df2;
990 define(dot.TOK_IDF, CurrentScope, D_LBOUND)) {
991 df1->bnd_type = tp; /* type conf. array */
992 df1->df_flags |= D_SET;
998 define(dot.TOK_IDF, CurrentScope, D_UBOUND)) {
999 df2->bnd_type = tp; /* type conf. array */
1000 df2->df_flags |= D_SET;
1003 ':' TypeIdentifier(ptp)
1004 { if( !bounded(*ptp) &&
1005 (*ptp)->tp_fund != T_INTEGER ) {
1006 error("Indextypespecification: illegal type");
1009 df1->df_type = df2->df_type = *ptp;