def.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* D E F I N I T I O N M E C H A N I S M */
  2. #include "debug.h"
  3. #include <alloc.h>
  4. #include <assert.h>
  5. #include <em_arith.h>
  6. #include <em_label.h>
  7. #include "LLlex.h"
  8. #include "def.h"
  9. #include "idf.h"
  10. #include "main.h"
  11. #include "misc.h"
  12. #include "node.h"
  13. #include "scope.h"
  14. #include "type.h"
  15. struct def *
  16. MkDef(id, scope, kind)
  17. register struct idf *id;
  18. register struct scope *scope;
  19. long kind;
  20. {
  21. /* Create a new definition structure in scope "scope", with
  22. * id "id" and kind "kind".
  23. */
  24. register struct def *df = new_def();
  25. df->df_idf = id;
  26. df->df_scope = scope;
  27. df->df_kind = kind;
  28. df->df_type = error_type;
  29. df->df_next = id->id_def;
  30. id->id_def = df;
  31. /* enter the definition in the list of definitions in this scope
  32. */
  33. df->df_nextinscope = scope->sc_def;
  34. scope->sc_def = df;
  35. return df;
  36. }
  37. struct def *
  38. define(id, scope, kind)
  39. register struct idf *id;
  40. register struct scope *scope;
  41. long kind;
  42. {
  43. /* Declare an identifier in a scope, but first check if it
  44. already has been defined.
  45. If so, then check for the cases in which this is legal,
  46. and otherwise give an error message.
  47. */
  48. register struct def *df;
  49. if( df = lookup(id, scope, 0L) ) {
  50. if (df->df_kind == D_INUSE) {
  51. if( kind != D_INUSE ) {
  52. error("\"%s\" already used in this block",
  53. id->id_text);
  54. }
  55. return MkDef(id, scope, kind);
  56. }
  57. if (df->df_kind == D_ERROR ) {
  58. /* used in forward references */
  59. df->df_kind = kind;
  60. return df;
  61. }
  62. /* other cases fit in an int (assume at least 2 bytes) */
  63. switch((int) df->df_kind ) {
  64. case D_LABEL :
  65. /* generate error message somewhere else */
  66. return NULLDEF;
  67. case D_PARAMETER :
  68. if( kind == D_VARIABLE )
  69. /* program parameter declared as variable */
  70. return df;
  71. break;
  72. case D_FORWTYPE :
  73. if( kind == D_FORWTYPE ) return df;
  74. if( kind == D_TYPE ) {
  75. /* forward reference resolved */
  76. df->df_kind = D_FTYPE;
  77. return df;
  78. }
  79. else
  80. error("identifier \"%s\" must be a type",
  81. id->id_text);
  82. return NULLDEF;
  83. case D_FWPROCEDURE :
  84. if( kind == D_PROCEDURE ) return df;
  85. error("procedure identification \"%s\" expected",
  86. id->id_text);
  87. return NULLDEF;
  88. case D_FWFUNCTION :
  89. if( kind == D_FUNCTION ) return df;
  90. error("function identification \"%s\" expected",
  91. id->id_text);
  92. return NULLDEF;
  93. }
  94. if( kind != D_ERROR )
  95. /* avoid spurious error messages */
  96. error("identifier \"%s\" already declared",id->id_text);
  97. return NULLDEF;
  98. }
  99. return MkDef(id, scope, kind);
  100. }
  101. DoDirective(directive, nd, tp, scl, function)
  102. struct idf *directive;
  103. struct node *nd;
  104. struct type *tp;
  105. struct scopelist *scl;
  106. {
  107. long kind; /* kind of directive */
  108. int inp; /* internal or external name */
  109. int ext = 0; /* directive = EXTERN */
  110. struct def *df = lookup(directive, PervasiveScope, D_INUSE);
  111. if( !df ) {
  112. if( !is_anon_idf(directive) )
  113. node_error(nd, "\"%s\" unknown directive",
  114. directive->id_text);
  115. return;
  116. }
  117. if (df->df_kind == D_FORWARD) {
  118. kind = function ? D_FWFUNCTION : D_FWPROCEDURE;
  119. inp = (proclevel > 1);
  120. }
  121. else if (df->df_kind == D_EXTERN) {
  122. kind = function ? D_FUNCTION : D_PROCEDURE;
  123. inp = 0;
  124. ext = 1;
  125. }
  126. else {
  127. node_error(nd, "\"%s\" unknown directive",
  128. directive->id_text);
  129. return;
  130. }
  131. if( df = define(nd->nd_IDF, CurrentScope, kind) ) {
  132. if( df->df_kind != kind ) {
  133. /* identifier already forward declared */
  134. node_error(nd, "\"%s\" already forward declared",
  135. nd->nd_IDF->id_text);
  136. return;
  137. }
  138. df->df_type = tp;
  139. df->prc_vis = scl;
  140. df->prc_name = gen_proc_name(nd->nd_IDF, inp);
  141. if( ext ) {
  142. if (!(df->df_flags & D_EXTERNAL) && proclevel > 1)
  143. tp->prc_nbpar -= pointer_size;
  144. /* was added for static link which is not needed now.
  145. But make sure this is done only once (look at the
  146. D_EXTERNAL flag).
  147. */
  148. df->df_flags |= D_EXTERNAL;
  149. }
  150. df->df_flags |= D_SET;
  151. }
  152. }
  153. struct def *
  154. DeclProc(nd, tp, scl)
  155. register struct node *nd;
  156. struct type *tp;
  157. register struct scopelist *scl;
  158. {
  159. register struct def *df;
  160. if( df = define(nd->nd_IDF, CurrentScope, D_PROCEDURE) ) {
  161. df->df_flags |= D_SET;
  162. if( df->df_kind == D_FWPROCEDURE ) {
  163. df->df_kind = D_PROCEDURE; /* identification */
  164. /* Simulate a call to open_scope(), which has already
  165. * been performed in the forward declaration.
  166. */
  167. CurrVis = df->prc_vis;
  168. if( tp->prc_params )
  169. node_error(nd,
  170. "\"%s\" already declared",
  171. nd->nd_IDF->id_text);
  172. }
  173. else { /* normal declaration */
  174. df->df_type = tp;
  175. df->prc_name = gen_proc_name(nd->nd_IDF, (proclevel>1));
  176. /* simulate open_scope() */
  177. CurrVis = df->prc_vis = scl;
  178. }
  179. routine_label(df);
  180. }
  181. else CurrVis = scl; /* simulate open_scope() */
  182. return df;
  183. }
  184. struct def *
  185. DeclFunc(nd, tp, scl)
  186. register struct node *nd;
  187. struct type *tp;
  188. register struct scopelist *scl;
  189. {
  190. register struct def *df;
  191. if( df = define(nd->nd_IDF, CurrentScope, D_FUNCTION) ) {
  192. df->df_flags &= ~D_SET;
  193. if( df->df_kind == D_FUNCTION ) { /* declaration */
  194. if( !tp ) {
  195. node_error(nd, "\"%s\" illegal function declaration",
  196. nd->nd_IDF->id_text);
  197. tp = construct_type(T_FUNCTION, error_type);
  198. }
  199. /* simulate open_scope() */
  200. CurrVis = df->prc_vis = scl;
  201. df->df_type = tp;
  202. df->prc_name = gen_proc_name(nd->nd_IDF, (proclevel > 1));
  203. }
  204. else { /* identification */
  205. assert(df->df_kind == D_FWFUNCTION);
  206. df->df_kind = D_FUNCTION;
  207. CurrVis = df->prc_vis;
  208. if( tp )
  209. node_error(nd,
  210. "\"%s\" already declared",
  211. nd->nd_IDF->id_text);
  212. }
  213. routine_label(df);
  214. }
  215. else CurrVis = scl; /* simulate open_scope() */
  216. return df;
  217. }
  218. EndFunc(df)
  219. register struct def *df;
  220. {
  221. /* assignment to functionname is illegal outside the functionblock */
  222. df->prc_res = 0;
  223. /* Give the error about assignment as soon as possible. The
  224. * |= assignment inhibits a warning in the main procedure.
  225. */
  226. if( !(df->df_flags & D_SET) ) {
  227. error("function \"%s\" not assigned",df->df_idf->id_text);
  228. df->df_flags |= D_SET;
  229. }
  230. }
  231. EndBlock(block_df)
  232. register struct def *block_df;
  233. {
  234. register struct def *tmp_def = CurrentScope->sc_def;
  235. register struct def *df;
  236. while( tmp_def ) {
  237. df = tmp_def;
  238. /* The length of a usd_def chain is at most 1.
  239. * The while is just defensive programming.
  240. */
  241. while( df->df_kind & D_INUSE )
  242. df = df->usd_def;
  243. if( !is_anon_idf(df->df_idf)
  244. && (df->df_scope == CurrentScope) ) {
  245. if( !(df->df_kind & (D_ENUM|D_LABEL|D_ERROR)) ) {
  246. if( !(df->df_flags & D_USED) ) {
  247. if( !(df->df_flags & D_SET) ) {
  248. warning("\"%s\" neither set nor used in \"%s\"",
  249. df->df_idf->id_text, block_df->df_idf->id_text);
  250. }
  251. else {
  252. warning("\"%s\" unused in \"%s\"",
  253. df->df_idf->id_text, block_df->df_idf->id_text);
  254. }
  255. }
  256. else if( !(df->df_flags & D_SET) ) {
  257. if( !(df->df_flags & D_LOOPVAR) )
  258. warning("\"%s\" not set in \"%s\"",
  259. df->df_idf->id_text, block_df->df_idf->id_text);
  260. }
  261. }
  262. }
  263. tmp_def = tmp_def->df_nextinscope;
  264. }
  265. }