ack/lang/m2/comp/declar.g

431 lines
7.6 KiB
Plaintext
Raw Normal View History

1986-03-26 15:11:02 +00:00
/* D E C L A R A T I O N S */
1986-03-20 14:52:03 +00:00
1986-03-26 15:11:02 +00:00
{
1986-03-20 14:52:03 +00:00
static char *RcsId = "$Header$";
1986-03-26 15:11:02 +00:00
#include <em_arith.h>
#include <em_label.h>
1986-03-26 22:46:48 +00:00
#include <assert.h>
1986-03-26 15:11:02 +00:00
#include "idf.h"
#include "misc.h"
#include "LLlex.h"
#include "def.h"
#include "type.h"
#include "scope.h"
}
1986-03-20 14:52:03 +00:00
1986-03-26 15:11:02 +00:00
ProcedureDeclaration
{
1986-03-26 22:46:48 +00:00
struct def *df;
1986-03-26 15:11:02 +00:00
} :
1986-03-26 22:46:48 +00:00
ProcedureHeading(&df, D_PROCEDURE)
1986-03-26 15:11:02 +00:00
';' block IDENT
{ match_id(dot.TOK_IDF, df->df_idf);
close_scope();
}
;
1986-03-26 22:46:48 +00:00
ProcedureHeading(struct def **pdf; int type;)
1986-03-26 15:11:02 +00:00
{
1986-03-27 17:37:41 +00:00
struct type *tp;
struct paramlist *params = 0;
1986-03-26 15:11:02 +00:00
} :
PROCEDURE IDENT
1986-03-29 01:04:49 +00:00
{ assert(type & (D_PROCEDURE | D_PROCHEAD));
1986-03-27 17:37:41 +00:00
*pdf = define(dot.TOK_IDF, CurrentScope, type);
1986-03-26 22:46:48 +00:00
if (type == D_PROCEDURE) {
open_scope(OPENSCOPE, 0);
}
}
1986-03-27 17:37:41 +00:00
FormalParameters(type, &params, &tp)?
1986-03-29 01:04:49 +00:00
{
(*pdf)->df_type = tp = construct_type(PROCEDURE, tp);
tp->prc_params = params;
}
1986-03-20 14:52:03 +00:00
;
block:
declaration* [ BEGIN StatementSequence ]? END
;
declaration:
CONST [ ConstantDeclaration ';' ]*
|
TYPE [ TypeDeclaration ';' ]*
|
VAR [ VariableDeclaration ';' ]*
|
ProcedureDeclaration ';'
|
ModuleDeclaration ';'
;
1986-03-27 17:37:41 +00:00
FormalParameters(int doparams; struct paramlist **pr; struct type **tp;)
{
struct def *df;
register struct paramlist *pr1;
} :
'('
[
1986-03-29 01:04:49 +00:00
FPSection(doparams, pr)
{ pr1 = *pr; }
1986-03-27 17:37:41 +00:00
[
1986-03-29 01:04:49 +00:00
{ for (; pr1->next; pr1 = pr1->next) ; }
1986-03-27 17:37:41 +00:00
';' FPSection(doparams, &(pr1->next))
]*
]?
')'
{ *tp = 0; }
1986-03-29 01:04:49 +00:00
[ ':' qualident(D_TYPE | D_HTYPE, &df, "type")
{ *tp = df->df_type; }
1986-03-26 15:11:02 +00:00
]?
1986-03-20 14:52:03 +00:00
;
1986-03-29 01:04:49 +00:00
/* In the next nonterminal, "doparams" is a flag indicating whether
the identifiers representing the parameters must be added to the
symbol table. We must not do so when reading a Definition Module,
because in this case we only read the header. The Implementation
might contain different identifiers representing the same paramters.
*/
1986-03-27 17:37:41 +00:00
FPSection(int doparams; struct paramlist **ppr;)
1986-03-20 14:52:03 +00:00
{
struct id_list *FPList;
1986-03-29 01:04:49 +00:00
struct paramlist *ParamList();
struct type *tp;
int VARp = 0;
1986-03-20 14:52:03 +00:00
} :
1986-03-26 15:11:02 +00:00
[
1986-03-29 01:04:49 +00:00
VAR { VARp = 1; }
1986-03-26 15:11:02 +00:00
]?
1986-03-29 01:04:49 +00:00
IdentList(&FPList) ':' FormalType(&tp)
{
if (doparams) {
EnterIdList(FPList, D_VARIABLE, VARp, tp, CurrentScope);
}
*ppr = ParamList(FPList, tp);
FreeIdList(FPList);
}
1986-03-20 14:52:03 +00:00
;
1986-03-29 01:04:49 +00:00
FormalType(struct type **tp;)
1986-03-27 17:37:41 +00:00
{
struct def *df;
int ARRAYflag = 0;
} :
[ ARRAY OF { ARRAYflag = 1; }
]?
qualident(D_TYPE | D_HTYPE, &df, "type")
1986-03-29 01:04:49 +00:00
{ if (ARRAYflag) {
*tp = construct_type(ARRAY, NULLTYPE);
(*tp)->arr_elem = df->df_type;
}
else *tp = df->df_type;
}
1986-03-20 14:52:03 +00:00
;
1986-03-26 15:11:02 +00:00
TypeDeclaration
{
1986-03-26 22:46:48 +00:00
struct def *df;
1986-03-27 17:37:41 +00:00
struct type *tp;
1986-03-26 15:11:02 +00:00
}:
1986-03-27 17:37:41 +00:00
IDENT { df = define(dot.TOK_IDF, CurrentScope, D_TYPE); }
'=' type(&tp)
1986-04-02 17:34:21 +00:00
{ df->df_type = tp;
if ((df->df_flags&D_EXPORTED) &&
tp->tp_fund == ENUMERATION) {
exprt_literals(tp->enm_enums, enclosing(currscope));
}
}
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
type(struct type **ptp;):
SimpleType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
ArrayType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
RecordType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
SetType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
PointerType(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
ProcedureType(ptp)
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
SimpleType(struct type **ptp;)
{
struct def *df;
1986-03-29 01:04:49 +00:00
struct type *tp;
1986-03-27 17:37:41 +00:00
} :
qualident(D_TYPE | D_HTYPE, &df, "type")
1986-03-20 14:52:03 +00:00
[
1986-03-29 01:04:49 +00:00
/* nothing */
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
SubrangeType(ptp)
1986-03-29 01:04:49 +00:00
/* The subrange type is given a base type by the
qualident (this is new modula-2).
*/
{
chk_basesubrange(*ptp, tp);
}
1986-03-20 14:52:03 +00:00
]
|
1986-03-27 17:37:41 +00:00
enumeration(ptp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
SubrangeType(ptp)
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
enumeration(struct type **ptp;)
1986-03-20 14:52:03 +00:00
{
struct id_list *EnumList;
} :
'(' IdentList(&EnumList) ')'
1986-03-26 22:46:48 +00:00
{
1986-03-27 17:37:41 +00:00
*ptp = standard_type(ENUMERATION,int_align,int_size);
EnterIdList(EnumList, D_ENUM, 0, *ptp, CurrentScope);
1986-03-26 22:46:48 +00:00
FreeIdList(EnumList);
}
1986-03-20 14:52:03 +00:00
;
IdentList(struct id_list **p;)
{
register struct id_list *q = new_id_list();
} :
1986-03-26 22:46:48 +00:00
IDENT { q->id_ptr = dot.TOK_IDF; *p = q;}
1986-03-20 14:52:03 +00:00
[
',' IDENT { q->next = new_id_list();
q = q->next;
q->id_ptr = dot.TOK_IDF;
}
]*
1986-03-26 22:46:48 +00:00
{ q->next = 0; }
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
SubrangeType(struct type **ptp;)
{
struct type *tp;
}:
1986-03-20 14:52:03 +00:00
/*
This is not exactly the rule in the new report, but see
the rule for "SimpleType".
*/
1986-03-27 17:37:41 +00:00
'[' ConstExpression
UPTO ConstExpression
']'
/*
Evaluate the expressions. Check that they are indeed constant.
???
Leave the basetype of the subrange in tp;
*/
{
/* For the time being: */
tp = int_type;
tp = construct_type(SUBRANGE, tp, (arith) 0);
*ptp = tp;
}
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
ArrayType(struct type **ptp;)
{
struct type *tp;
register struct type *tp2;
} :
ARRAY SimpleType(&tp)
{
*ptp = tp2 = construct_type(ARRAY, tp);
}
[
',' SimpleType(&tp)
1986-03-29 01:04:49 +00:00
{ tp2 = tp2->arr_elem =
1986-03-27 17:37:41 +00:00
construct_type(ARRAY, tp);
}
]* OF type(&tp)
1986-03-29 01:04:49 +00:00
{ tp2->arr_elem = tp; }
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
RecordType(struct type **ptp;)
{
int scopenr;
}
:
RECORD
{ scopenr = uniq_scope(); }
FieldListSequence(scopenr)
{
*ptp = standard_type(RECORD, record_align, (arith) 0 /* ???? */);
1986-03-29 01:04:49 +00:00
(*ptp)->rec_scopenr = scopenr;
1986-03-27 17:37:41 +00:00
}
END
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FieldListSequence(int scopenr;):
FieldList(scopenr)
[
';' FieldList(scopenr)
]*
1986-03-20 14:52:03 +00:00
;
1986-03-27 17:37:41 +00:00
FieldList(int scopenr;)
1986-03-20 14:52:03 +00:00
{
struct id_list *FldList;
1986-03-27 17:37:41 +00:00
struct idf *id;
struct def *df, *df1;
struct type *tp;
1986-03-20 14:52:03 +00:00
} :
[
1986-03-27 17:37:41 +00:00
IdentList(&FldList) ':' type(&tp)
1986-03-20 14:52:03 +00:00
|
1986-03-27 17:37:41 +00:00
CASE
[
IDENT { id = dot.TOK_IDF; }
|
{ id = gen_anon_idf(); }
] /* Changed rule in new modula-2 */
':' qualident(D_TYPE|D_HTYPE, &df, "type")
{ df1 = define(id, scopenr, D_FIELD);
df1->df_type = df->df_type;
}
OF variant(scopenr)
[
'|' variant(scopenr)
]*
[ ELSE FieldListSequence(scopenr)
]?
1986-03-20 14:52:03 +00:00
END
]?
;
1986-03-27 17:37:41 +00:00
variant(int scopenr;):
[ CaseLabelList ':' FieldListSequence(scopenr) ]?
1986-03-20 14:52:03 +00:00
/* Changed rule in new modula-2 */
;
CaseLabelList:
CaseLabels [ ',' CaseLabels ]*
;
CaseLabels:
ConstExpression [ UPTO ConstExpression ]?
;
1986-03-27 17:37:41 +00:00
SetType(struct type **ptp;)
{
struct type *tp;
} :
SET OF SimpleType(&tp)
{
*ptp = construct_type(SET, tp, (arith) 0 /* ???? */);
}
1986-03-20 14:52:03 +00:00
;
1986-03-29 01:04:49 +00:00
/* In a pointer type definition, the type pointed at does not
have to be declared yet, so be careful about identifying
type-identifiers
*/
1986-03-27 17:37:41 +00:00
PointerType(struct type **ptp;)
{
struct type *tp;
1986-03-29 01:04:49 +00:00
struct def *df;
1986-03-27 17:37:41 +00:00
struct def *lookfor();
} :
POINTER TO
[ %if ( (df = lookup(dot.TOK_IDF, CurrentScope)))
1986-03-29 01:04:49 +00:00
/* Either a Module or a Type, but in both cases defined
in this scope, so this is the correct identification
*/
qualident(D_TYPE|D_HTYPE, &df, "type")
1986-03-27 17:37:41 +00:00
{
if (!df->df_type) {
error("type \"%s\" not declared",
df->df_idf->id_text);
1986-03-29 01:04:49 +00:00
tp = error_type;
1986-03-27 17:37:41 +00:00
}
1986-03-29 01:04:49 +00:00
else tp = df->df_type;
1986-03-27 17:37:41 +00:00
}
1986-04-02 17:34:21 +00:00
| %if (df = lookfor(dot.TOK_IDF, currscope, 0), df->df_kind == D_MODULE)
1986-03-27 17:37:41 +00:00
type(&tp)
|
IDENT
1986-03-29 01:04:49 +00:00
{ tp = NULLTYPE; }
1986-03-27 17:37:41 +00:00
]
1986-03-29 01:04:49 +00:00
{
*ptp = construct_type(POINTER, tp);
if (!tp) Forward(&dot, &((*ptp)->next));
}
1986-03-20 14:52:03 +00:00
;
1986-03-29 01:04:49 +00:00
ProcedureType(struct type **ptp;)
{
struct paramlist *pr = 0;
struct type *tp = 0;
} :
PROCEDURE FormalTypeList(&pr, &tp)?
{ *ptp = construct_type(PROCEDURE, tp);
(*ptp)->prc_params = pr;
}
1986-03-20 14:52:03 +00:00
;
1986-03-29 01:04:49 +00:00
FormalTypeList(struct paramlist **ppr; struct type **ptp;)
1986-03-27 17:37:41 +00:00
{
struct def *df;
1986-03-29 01:04:49 +00:00
struct type *tp;
struct paramlist *p;
int VARp;
1986-03-27 17:37:41 +00:00
} :
1986-03-29 01:04:49 +00:00
'(' { *ppr = 0; }
[
[ VAR { VARp = 1; }
| { VARp = 0; }
]
FormalType(&tp)
{ *ppr = p = new_paramlist();
p->par_type = tp;
p->par_var = VARp;
}
[
','
[ VAR {VARp = 1; }
| {VARp = 0; }
]
FormalType(&tp)
{ p->next = new_paramlist();
p = p->next;
p->par_type = tp;
p->par_var = VARp;
}
]*
{ p->next = 0; }
]?
')'
[ ':' qualident(D_TYPE|D_HTYPE, &df, "type")
{ *ptp = df->df_type; }
1986-03-27 17:37:41 +00:00
]?
1986-03-20 14:52:03 +00:00
;
1986-03-26 15:11:02 +00:00
ConstantDeclaration
{
1986-03-26 22:46:48 +00:00
struct def *df;
struct idf *id;
1986-03-26 15:11:02 +00:00
}:
1986-03-26 22:46:48 +00:00
IDENT { id = dot.TOK_IDF; }
'=' ConstExpression { df = define(id, CurrentScope, D_CONST);
/* ???? */
}
1986-03-20 14:52:03 +00:00
;
VariableDeclaration
{
struct id_list *VarList;
1986-03-27 17:37:41 +00:00
struct type *tp;
1986-03-20 14:52:03 +00:00
} :
1986-03-24 17:29:57 +00:00
IdentList(&VarList)
[
ConstExpression
]?
1986-03-27 17:37:41 +00:00
':' type(&tp)
{ EnterIdList(VarList, D_VARIABLE, 0, tp, CurrentScope);
1986-03-26 22:46:48 +00:00
FreeIdList(VarList);
}
1986-03-20 14:52:03 +00:00
;