symbol.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. s = Salloc(s, (unsigned) strlen(s)+1);
  106. *p = c;
  107. sym1 = NewSymbol(s,
  108. PervasiveScope,
  109. FILELINK,
  110. (struct outname *) 0);
  111. sym1->sy_filelink = sym;
  112. sym->sy_file->f_base = sym1;
  113. }
  114. return sym;
  115. }
  116. static p_scope
  117. def_scope(s)
  118. p_symbol s;
  119. {
  120. switch(s->sy_class) {
  121. case FILELINK:
  122. s = s->sy_filelink;
  123. /* fall through */
  124. case FILESYM:
  125. return s->sy_file->f_scope;
  126. case PROC:
  127. case FUNCTION:
  128. case MODULE:
  129. case TYPE:
  130. case VAR:
  131. case REGVAR:
  132. case LOCVAR:
  133. case VARPAR:
  134. return s->sy_name.nm_scope;
  135. }
  136. return 0;
  137. }
  138. /* Determine if the OP_SELECT tree indicated by 'p' could lead to scope 'sc'.
  139. */
  140. int
  141. consistent(p, sc)
  142. p_tree p;
  143. p_scope sc;
  144. {
  145. p_tree arg;
  146. p_symbol sym;
  147. p_scope target_sc;
  148. assert(p->t_oper == OP_SELECT);
  149. p = p->t_args[0];
  150. switch(p->t_oper) {
  151. case OP_NAME:
  152. #define CLASS (FILELINK|FILESYM|PROC|FUNCTION|MODULE|TYPE|VAR|REGVAR|LOCVAR|VARPAR|LBOUND|UBOUND)
  153. sym = Lookfromscope(p->t_idf, CLASS, sc->sc_static_encl);
  154. if (sym) {
  155. int precise = 1;
  156. target_sc = def_scope(sym);
  157. while (sc && sc != target_sc) {
  158. precise = 0;
  159. sc = sc->sc_static_encl;
  160. }
  161. return sc == 0 ? 0 : precise + 1 ;
  162. }
  163. return 0;
  164. case OP_SELECT:
  165. arg = p->t_args[1];
  166. sym = Lookfromscope(arg->t_idf, CLASS, sc->sc_static_encl);
  167. if (sym) {
  168. int precise = 1;
  169. target_sc = def_scope(sym);
  170. while (sc && sc != target_sc) {
  171. precise = 0;
  172. sc = sc->sc_static_encl;
  173. }
  174. if (sc == 0) return 0;
  175. if (precise) return consistent(p, sym->sy_scope);
  176. return consistent(p, sym->sy_scope) != 0;
  177. }
  178. return 0;
  179. default:
  180. assert(0);
  181. }
  182. return 0; /* notreached? */
  183. }
  184. /* Try to find the name referred to in the node indicated by 'p', and
  185. try to be just a little bit intelligent about it.
  186. */
  187. p_symbol
  188. identify(p, class_set)
  189. p_tree p;
  190. int class_set;
  191. {
  192. p_symbol sym = 0, sym1 = 0;
  193. register p_symbol s;
  194. p_tree arg;
  195. int precise = 0;
  196. switch(p->t_oper) {
  197. case OP_NAME:
  198. sym = Lookfromscope(p->t_idf, class_set, CurrentScope);
  199. if (sym) {
  200. /* Found it. */
  201. break;
  202. }
  203. /* We could not find it using the current scope; now we try to identify
  204. it using class_set. If this results in only one definition, we
  205. take this one.
  206. */
  207. s = p->t_idf->id_def;
  208. while (s) {
  209. if (s->sy_class & class_set) {
  210. if (sym) {
  211. error("could not identify \"%s\"", p->t_str);
  212. sym = 0;
  213. break;
  214. }
  215. sym = s;
  216. }
  217. s = s->sy_next;
  218. }
  219. if (!sym && !s) {
  220. error("could not find \"%s\"", p->t_str);
  221. }
  222. break;
  223. case OP_SELECT:
  224. arg = p->t_args[1];
  225. assert(arg->t_oper == OP_NAME);
  226. s = arg->t_idf->id_def;
  227. while (s) {
  228. int temp;
  229. if ((s->sy_class & class_set) &&
  230. (temp = consistent(p, s->sy_scope))) {
  231. if (temp > precise) {
  232. sym = s;
  233. precise = temp;
  234. sym1 = 0;
  235. }
  236. else if (sym && temp == precise) sym1 = s;
  237. }
  238. s = s->sy_next;
  239. }
  240. if (sym && sym1) {
  241. error("could not identify \"%s\"", arg->t_str);
  242. return 0;
  243. }
  244. if (!sym && !s) {
  245. error("could not find \"%s\"", arg->t_str);
  246. return 0;
  247. }
  248. break;
  249. default:
  250. assert(0);
  251. }
  252. return sym;
  253. }
  254. static
  255. pr_scopes(sc)
  256. p_scope sc;
  257. {
  258. while (sc && ! sc->sc_definedby) {
  259. sc = sc->sc_static_encl;
  260. }
  261. if (sc) {
  262. pr_scopes(sc->sc_static_encl);
  263. if (sc->sc_definedby->sy_class == FILESYM &&
  264. sc->sc_definedby->sy_file->f_base) {
  265. fprintf(db_out, "%s`", sc->sc_definedby->sy_file->f_base->sy_idf->id_text);
  266. }
  267. else fprintf(db_out, "%s`", sc->sc_definedby->sy_idf->id_text);
  268. }
  269. }
  270. pr_sym(s)
  271. p_symbol s;
  272. {
  273. switch(s->sy_class) {
  274. case CONST:
  275. fprintf(db_out, "Constant:\t");
  276. break;
  277. case TYPE:
  278. fprintf(db_out, "Type:\t\t");
  279. break;
  280. case TAG:
  281. fprintf(db_out, "Tag:\t\t");
  282. break;
  283. case MODULE:
  284. fprintf(db_out, "Module:\t\t");
  285. break;
  286. case PROC:
  287. case FUNCTION:
  288. fprintf(db_out, "Routine:\t");
  289. break;
  290. case VAR:
  291. case REGVAR:
  292. case LOCVAR:
  293. case VARPAR:
  294. case LBOUND:
  295. case UBOUND:
  296. fprintf(db_out, "Variable:\t");
  297. break;
  298. case FIELD:
  299. fprintf(db_out, "Field:\t\t");
  300. break;
  301. case FILESYM:
  302. case FILELINK:
  303. fprintf(db_out, "File:\t\t");
  304. break;
  305. default:
  306. assert(0);
  307. }
  308. pr_scopes(s->sy_scope);
  309. fprintf(db_out, "%s\n", s->sy_idf->id_text);
  310. }
  311. resolve_cross(tp)
  312. p_type tp;
  313. {
  314. register p_symbol sym = tp->ty_sym->sy_idf->id_def;
  315. while (sym) {
  316. if (sym->sy_class == TAG &&
  317. sym->sy_type->ty_class == T_CROSS &&
  318. sym->sy_type->ty_cross == (p_type) 0 &&
  319. sym->sy_type->ty_size == tp->ty_class &&
  320. scope_encloses(tp->ty_sym->sy_scope, sym->sy_scope)) {
  321. sym->sy_type->ty_cross = tp;
  322. sym->sy_type->ty_size = tp->ty_size;
  323. }
  324. sym = sym->sy_next;
  325. }
  326. }