symbol.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* $Header$ */
  2. /* Symbol handling */
  3. #include <stdio.h>
  4. #include <alloc.h>
  5. #include <out.h>
  6. #include <stb.h>
  7. #include <assert.h>
  8. #include "position.h"
  9. #include "file.h"
  10. #include "idf.h"
  11. #include "type.h"
  12. #include "symbol.h"
  13. #include "scope.h"
  14. #include "tree.h"
  15. #include "operator.h"
  16. p_symbol currfile, listfile;
  17. extern FILE *db_out;
  18. p_symbol
  19. NewSymbol(s, scope, class, nam)
  20. char *s;
  21. register p_scope scope;
  22. struct outname *nam;
  23. {
  24. register p_symbol sym;
  25. sym = new_symbol();
  26. sym->sy_idf = str2idf(s, 0);
  27. sym->sy_scope = scope;
  28. sym->sy_prev_sc = scope->sc_symbs;
  29. scope->sc_symbs = sym;
  30. sym->sy_next = sym->sy_idf->id_def;
  31. sym->sy_idf->id_def = sym;
  32. sym->sy_class = class;
  33. switch(class) {
  34. case MODULE:
  35. case PROC:
  36. case FUNCTION:
  37. case VAR:
  38. case REGVAR:
  39. case LOCVAR:
  40. case VARPAR:
  41. sym->sy_name.nm_value = nam->on_valu;
  42. break;
  43. default:
  44. break;
  45. }
  46. return sym;
  47. }
  48. /* Lookup a definition for 'id' in scope 'scope' with class in the 'class'
  49. bitset.
  50. */
  51. p_symbol
  52. Lookup(id, scope, class)
  53. struct idf *id;
  54. p_scope scope;
  55. int class;
  56. {
  57. register p_symbol p = id ? id->id_def : 0;
  58. while (p) {
  59. if (p->sy_scope == scope && (p->sy_class & class)) {
  60. return p;
  61. }
  62. p = p->sy_next;
  63. }
  64. return (p_symbol) 0;
  65. }
  66. /* Lookup a definition for 'id' with class in the 'class' bitset,
  67. starting in scope 'sc' and also looking in enclosing scopes.
  68. */
  69. p_symbol
  70. Lookfromscope(id, class, sc)
  71. register struct idf *id;
  72. int class;
  73. register p_scope sc;
  74. {
  75. if (! id) return (p_symbol) 0;
  76. while (sc) {
  77. register p_symbol sym = id->id_def;
  78. while (sym) {
  79. if (sym->sy_scope == sc && (sym->sy_class & class)) {
  80. return sym;
  81. }
  82. sym = sym->sy_next;
  83. }
  84. sc = sc->sc_static_encl;
  85. }
  86. return (p_symbol) 0;
  87. }
  88. extern char *strrindex();
  89. p_symbol
  90. add_file(s)
  91. char *s;
  92. {
  93. register p_symbol sym = NewSymbol(s,
  94. PervasiveScope,
  95. FILESYM,
  96. (struct outname *) 0);
  97. register char *p;
  98. sym->sy_file = new_file();
  99. sym->sy_file->f_sym = sym;
  100. p = strrindex(s, '.');
  101. if (p) {
  102. char c = *p;
  103. p_symbol sym1;
  104. *p = 0;
  105. sym1 = NewSymbol(Salloc(s, (unsigned) strlen(s)+1),
  106. PervasiveScope,
  107. FILELINK,
  108. (struct outname *) 0);
  109. *p = c;
  110. sym1->sy_filelink = sym;
  111. sym->sy_file->f_base = sym1;
  112. }
  113. return sym;
  114. }
  115. p_scope
  116. def_scope(s)
  117. p_symbol s;
  118. {
  119. switch(s->sy_class) {
  120. case FILELINK:
  121. s = s->sy_filelink;
  122. /* fall through */
  123. case FILESYM:
  124. return s->sy_file->f_scope;
  125. case PROC:
  126. case FUNCTION:
  127. case MODULE:
  128. case TYPE:
  129. case VAR:
  130. case REGVAR:
  131. case LOCVAR:
  132. case VARPAR:
  133. return s->sy_name.nm_scope;
  134. }
  135. return 0;
  136. }
  137. /* Determine if the OP_SELECT tree indicated by 'p' could lead to scope 'sc'.
  138. */
  139. static int
  140. consistent(p, sc)
  141. p_tree p;
  142. p_scope sc;
  143. {
  144. p_tree arg;
  145. p_symbol sym;
  146. p_scope target_sc;
  147. assert(p->t_oper == OP_SELECT);
  148. p = p->t_args[0];
  149. switch(p->t_oper) {
  150. case OP_NAME:
  151. #define CLASS (FILELINK|FILESYM|PROC|FUNCTION|MODULE|TYPE|VAR|REGVAR|LOCVAR|VARPAR)
  152. sym = Lookfromscope(p->t_idf, CLASS, sc->sc_static_encl);
  153. if (sym) {
  154. target_sc = def_scope(sym);
  155. while (sc && sc != target_sc) {
  156. sc = sc->sc_static_encl;
  157. }
  158. return sc != 0;
  159. }
  160. return 0;
  161. case OP_SELECT:
  162. arg = p->t_args[1];
  163. sym = Lookfromscope(arg->t_idf, CLASS, sc->sc_static_encl);
  164. if (sym) {
  165. target_sc = def_scope(sym);
  166. while (sc && sc != target_sc) {
  167. sc = sc->sc_static_encl;
  168. }
  169. return sc != 0 && consistent(p, sym->sy_scope);
  170. }
  171. return 0;
  172. default:
  173. assert(0);
  174. }
  175. return 0; /* notreached? */
  176. }
  177. /* Try to find the name referred to in the node indicated by 'p', and
  178. try to be just a little bit intelligent about it.
  179. */
  180. p_symbol
  181. identify(p, class_set)
  182. p_tree p;
  183. int class_set;
  184. {
  185. p_symbol sym = 0;
  186. register p_symbol s;
  187. p_tree arg;
  188. switch(p->t_oper) {
  189. case OP_NAME:
  190. if (! p->t_sc) p->t_sc = CurrentScope;
  191. sym = Lookfromscope(p->t_idf, class_set, p->t_sc);
  192. if (sym) {
  193. /* Found it. */
  194. break;
  195. }
  196. /* We could not find it using scope p->t_sc; now we try to identify
  197. it using class_set. If this results in only one definition, we
  198. take this one.
  199. */
  200. s = p->t_idf->id_def;
  201. while (s) {
  202. if (s->sy_class & class_set) {
  203. if (sym) {
  204. error("could not identify \"%s\"", p->t_str);
  205. sym = 0;
  206. break;
  207. }
  208. sym = s;
  209. }
  210. s = s->sy_next;
  211. }
  212. if (!sym && !s) {
  213. error("could not find \"%s\"", p->t_str);
  214. }
  215. break;
  216. case OP_SELECT:
  217. arg = p->t_args[1];
  218. assert(arg->t_oper == OP_NAME);
  219. s = arg->t_idf->id_def;
  220. sym = 0;
  221. while (s) {
  222. if ((s->sy_class & class_set) && consistent(p, s->sy_scope)) {
  223. if (sym) {
  224. error("could not identify \"%s\"", arg->t_str);
  225. sym = 0;
  226. }
  227. sym = s;
  228. }
  229. s = s->sy_next;
  230. }
  231. if (!sym && !s) {
  232. error("could not find \"%s\"", arg->t_str);
  233. }
  234. break;
  235. default:
  236. assert(0);
  237. }
  238. return sym;
  239. }
  240. static
  241. pr_scopes(sc)
  242. p_scope sc;
  243. {
  244. while (sc && ! sc->sc_definedby) {
  245. sc = sc->sc_static_encl;
  246. }
  247. if (sc) {
  248. pr_scopes(sc->sc_static_encl);
  249. if (sc->sc_definedby->sy_class == FILESYM &&
  250. sc->sc_definedby->sy_file->f_base) {
  251. fprintf(db_out, "%s`", sc->sc_definedby->sy_file->f_base->sy_idf->id_text);
  252. }
  253. else fprintf(db_out, "%s`", sc->sc_definedby->sy_idf->id_text);
  254. }
  255. }
  256. static
  257. pr_sym(s)
  258. p_symbol s;
  259. {
  260. switch(s->sy_class) {
  261. case CONST:
  262. fprintf(db_out, "Constant:\t");
  263. break;
  264. case TYPE:
  265. fprintf(db_out, "Type:\t\t");
  266. break;
  267. case TAG:
  268. fprintf(db_out, "Tag:\t\t");
  269. break;
  270. case MODULE:
  271. fprintf(db_out, "Module:\t\t");
  272. break;
  273. case PROC:
  274. case FUNCTION:
  275. fprintf(db_out, "Routine:\t");
  276. break;
  277. case VAR:
  278. case REGVAR:
  279. case LOCVAR:
  280. case VARPAR:
  281. fprintf(db_out, "Variable:\t");
  282. break;
  283. case FIELD:
  284. fprintf(db_out, "Field:\t\t");
  285. break;
  286. case FILESYM:
  287. case FILELINK:
  288. fprintf(db_out, "File:\t\t");
  289. break;
  290. default:
  291. assert(0);
  292. }
  293. pr_scopes(s->sy_scope);
  294. fprintf(db_out, "%s\n", s->sy_idf->id_text);
  295. }
  296. /* Print all identifications of p->t_args[0].
  297. */
  298. do_find(p)
  299. p_tree p;
  300. {
  301. register p_symbol s;
  302. p_tree arg;
  303. p = p->t_args[0];
  304. switch(p->t_oper) {
  305. case OP_NAME:
  306. s = p->t_idf->id_def;
  307. while (s) {
  308. pr_sym(s);
  309. s = s->sy_next;
  310. }
  311. break;
  312. case OP_SELECT:
  313. arg = p->t_args[1];
  314. assert(arg->t_oper == OP_NAME);
  315. s = arg->t_idf->id_def;
  316. while (s) {
  317. if (consistent(p, s->sy_scope)) {
  318. pr_sym(s);
  319. }
  320. s = s->sy_next;
  321. }
  322. break;
  323. default:
  324. assert(0);
  325. }
  326. }
  327. do_which(p)
  328. p_tree p;
  329. {
  330. p_symbol sym = identify(p->t_args[0], 0xffff);
  331. if ( sym) pr_sym(sym);
  332. }