program.g 6.2 KB

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