Pristine Ack-5.5
[Ack-5.5.git] / lang / m2 / comp / program.g
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7
8 /* O V E R A L L   S T R U C T U R E */
9
10 /* $Id: program.g,v 1.72 1997/02/21 17:10:13 ceriel Exp $ */
11
12 {
13 #include        "debug.h"
14
15 #include        <alloc.h>
16 #include        <em_arith.h>
17 #include        <em_label.h>
18 #include        <em_code.h>
19 #include        <stb.h>
20
21 #include        "dbsymtab.h"
22 #include        "strict3rd.h"
23 #include        "main.h"
24 #include        "idf.h"
25 #include        "LLlex.h"
26 #include        "scope.h"
27 #include        "def.h"
28 #include        "type.h"
29 #include        "node.h"
30 #include        "f_info.h"
31 #include        "warning.h"
32 #include        "misc.h"
33
34 extern t_def    *GetDefinitionModule();
35
36 }
37 /*
38         The grammar as given by Wirth is already almost LL(1); the
39         main problem is that the full form of a qualified designator
40         may be:
41                 [ module_ident '.' ]* IDENT [ '.' field_ident ]*
42         which is quite confusing to an LL(1) parser. Rather than
43         resorting to context-sensitive techniques, I have decided
44         to render this as:
45                 IDENT [ '.' IDENT ]*
46         on the grounds that it is quite natural to consider the first
47         IDENT to be the name of the object and regard the others as
48         field identifiers.
49 */
50
51 %lexical LLlex;
52
53 %start  CompUnit, CompilationUnit;
54 %start  DefModule, DefinitionModule;
55
56 ModuleDeclaration
57 {
58         register t_def  *df;
59         t_node          *exportlist;
60         int             qualified;
61 } :
62         MODULE IDENT    { df = DefineLocalModule(dot.TOK_IDF); }
63         priority(&(df->mod_priority))
64         ';'
65         import(1)*
66         export(&qualified, &exportlist)
67         block(&(df->mod_body))
68         IDENT           { EnterExportList(exportlist, qualified);
69                           close_scope(SC_CHKFORW|SC_CHKPROC|SC_REVERSE);
70                           match_id(df->df_idf, dot.TOK_IDF);
71                         }
72 ;
73
74 priority(register t_node **prio;):
75         [
76                 '[' ConstExpression(prio) ']'
77                         { if (! ((*prio)->nd_type->tp_fund & T_CARDINAL)) {
78                                 node_error(*prio, "illegal priority");
79                           }
80                         }
81         |
82         ]
83 ;
84
85 export(int *QUALflag; t_node **ExportList;):
86                                 { *ExportList = 0; *QUALflag = D_EXPORTED; }
87         [
88                 EXPORT
89                 [
90                         QUALIFIED
91                                 { *QUALflag = D_QEXPORTED; }
92                 |
93                 ]
94                 IdentList(ExportList) ';'
95         |
96         ]
97 ;
98
99 import(int local;)
100 {
101         t_node          *ImportList;
102         register t_node *FromId = 0;
103         register t_def  *df;
104 } :
105         /*
106            When parsing a global module, this is the place where we must
107            read already compiled definition modules.
108            If the FROM clause is present, the identifier in it is a module
109            name, otherwise the names in the import list are module names.
110         */
111         [ FROM
112           IDENT
113           %substart DefinitionModule;
114                         { FromId = dot2leaf(Name);
115                           if (local) {
116                                 df = lookfor(FromId,enclosing(CurrVis),0,D_USED);
117                           }
118                           else df = GetDefinitionModule(dot.TOK_IDF, 1);
119                         }
120         ]?
121         IMPORT IdentList(&ImportList) ';'
122           %substart DefinitionModule;
123                         { if (FromId) {
124                                 EnterFromImportList(ImportList, df, FromId);
125                           }
126                           else {
127                                 EnterImportList(ImportList,
128                                                 local,
129                                                 enclosing(CurrVis)->sc_scope);
130                           }
131                           FreeNode(ImportList);
132                         }
133 ;
134
135 DefinitionModule
136 {
137         register t_def  *df;
138         t_node          *exportlist;
139         int             dummy;
140         extern t_idf    *DefId;
141         extern int      ForeignFlag;
142         extern char     *sprint();
143         register t_scope *currscope = CurrentScope;
144         char buf[512];
145 } :
146         DEFINITION
147         MODULE IDENT    { df = define(dot.TOK_IDF, GlobalScope, D_MODULE);
148                           df->df_flags |= D_BUSY | ForeignFlag;
149                           currscope->sc_definedby = df;
150                           if (DefId &&
151                               df->df_idf != DefId &&
152                               !is_anon_idf(df->df_idf)) {
153                                 error("DEFINITION MODULE name is \"%s\", not \"%s\"",
154                                         df->df_idf->id_text, DefId->id_text);
155                           }
156                           sprint(buf, "_%s_", df->df_idf->id_text);
157                           currscope->sc_name = Salloc(buf, (unsigned) strlen(buf) + 1);
158                           df->mod_vis = CurrVis;
159                           df->df_type = standard_type(T_RECORD, 1, (arith) 1);
160                           df->df_type->rec_scope = currscope;
161                           DefinitionModule++;
162                           if (!Defined) {
163                                 Defined = df;
164                           }
165                         }
166         ';'
167         import(0)* 
168         export(&dummy, &exportlist)
169                 /*      New Modula-2 does not have export lists in definition
170                         modules. Issue a warning.
171                 */
172                         { 
173                           if (exportlist) {
174 #ifndef STRICT_3RD_ED
175                             if (! options['3'])
176 node_warning(exportlist, W_OLDFASHIONED, "export list in definition module ignored");
177                             else
178 #endif
179                                 error("export list not allowed in definition module");
180                             FreeNode(exportlist);
181                           }
182                         }
183         definition* END IDENT
184                         { end_definition_list(&(currscope->sc_def));
185                           DefinitionModule--;
186                           match_id(dot.TOK_IDF, df->df_idf);
187                           df->df_flags &= ~D_BUSY;
188                         }
189         '.'
190 ;
191
192 definition
193 {
194         register t_def  *df;
195         t_def           *dummy;
196 } :
197         CONST [ %persistent ConstantDeclaration ';' ]*
198 |
199         TYPE
200         [ %persistent
201           IDENT         { df = define(dot.TOK_IDF, CurrentScope, D_TYPE); }
202           [ '=' type(&(df->df_type))
203                         { SolveForwardTypeRefs(df); }
204           | /* empty */
205             /*
206                Here, the exported type has a hidden implementation.
207                The export is said to be opaque.
208                It is restricted to pointer types.
209             */
210                         { df->df_kind = D_HIDDEN;
211                           df->df_type = construct_type(T_HIDDEN, NULLTYPE);
212                         }
213           ]
214                         {
215 #ifdef DBSYMTAB
216                                 if (options['g']) stb_string(df, D_TYPE);
217 #endif
218                         }
219           ';'
220         ]*
221 |
222         VAR [ %persistent VariableDeclaration ';' ]*
223 |
224         ProcedureHeading(&dummy, D_PROCHEAD) ';'
225 ;
226
227 ProgramModule
228 {
229         register t_def  *df;
230 } :
231         MODULE
232         IDENT
233         %substart DefinitionModule;
234                 { 
235                   if (state == IMPLEMENTATION) {
236                         int len = strlen(dot.TOK_IDF->id_text);
237
238                         if (len > 10) len = 10;
239                         if (strncmp(FileName, dot.TOK_IDF->id_text, len)) {
240                                 warning(W_ORDINARY, "modulename %s does not match filename %s", dot.TOK_IDF->id_text, FileName);
241                         }
242                         df = GetDefinitionModule(dot.TOK_IDF, 0);
243                         CurrVis = df->mod_vis;
244                   }
245                   else {
246                         Defined = df = define(dot.TOK_IDF, GlobalScope, D_MODULE);
247                         open_scope(CLOSEDSCOPE);
248                         df->mod_vis = CurrVis;
249                         CurrentScope->sc_name = "__M2M_";
250                         CurrentScope->sc_definedby = df;
251                   }
252                 }
253         priority(&(df->mod_priority))
254         ';' import(0)*
255         block(&(df->mod_body)) IDENT
256                 {
257                   close_scope(SC_CHKFORW|SC_CHKPROC|SC_REVERSE);
258                   match_id(df->df_idf, dot.TOK_IDF);
259                 }
260         '.'
261 ;
262
263 CompilationUnit:
264                         { error("Compiling a definition module");
265                           open_scope(CLOSEDSCOPE);
266                           state = DEFINITION;
267                         }
268         DefinitionModule
269                         { close_scope(SC_CHKFORW); }
270 |       %default
271         [
272                 IMPLEMENTATION
273                         { state = IMPLEMENTATION; }
274         |
275                 /* empty */
276                         { state = PROGRAM; }
277         ]
278         ProgramModule
279 ;