l_outdef.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. /* $Header$ */
  6. /* Lint outdef construction */
  7. #include "lint.h"
  8. #ifdef LINT
  9. #include <alloc.h>
  10. #include "arith.h"
  11. #include "type.h"
  12. #include "LLlex.h"
  13. #include "Lpars.h"
  14. #include "stack.h"
  15. #include "def.h"
  16. #include "struct.h"
  17. #include "field.h"
  18. #include "idf.h"
  19. #include "level.h"
  20. #include "label.h"
  21. #include "expr.h"
  22. #include "l_lint.h"
  23. #include "l_outdef.h"
  24. #include "l_class.h"
  25. extern char *symbol2str();
  26. extern int f_VARARGSn, LINTLIB;
  27. int stat_number = 9999; /* static scope number */
  28. struct outdef OutDef, OutCall;
  29. lint_declare_idf(idf, sc)
  30. struct idf *idf;
  31. int sc;
  32. {
  33. register struct def *def = idf->id_def;
  34. register int is_function = def->df_type->tp_fund == FUNCTION;
  35. if (level == L_GLOBAL) {
  36. lint_ext_def(idf, sc);
  37. if (is_function)
  38. def2decl(sc);
  39. if (sc != TYPEDEF)
  40. outdef();
  41. }
  42. else
  43. if (level >= L_LOCAL && sc != STATIC && is_function) {
  44. local_EFDC(idf);
  45. }
  46. }
  47. lint_ext_def(idf, sc)
  48. struct idf *idf;
  49. {
  50. /* At this place the following fields of the outputdefinition can be
  51. * filled:
  52. * name, stat_number, class, file, line, type.
  53. * For variable definitions and declarations this will be all.
  54. * For functions the fields nrargs and argtps are filled after parsing
  55. * the arguments.
  56. * The returns-field is known at the end of the function definition.
  57. * sc indicates the storage class defined by the declaration specifier.
  58. */
  59. register struct def *def = idf->id_def;
  60. register struct type *type = def->df_type;
  61. OutDef.od_name = idf->id_text;
  62. OutDef.od_statnr = (sc == STATIC ? stat_number : 0);
  63. switch (type->tp_fund) {
  64. case ERRONEOUS:
  65. OutDef.od_class = XXDF;
  66. break;
  67. case FUNCTION:
  68. /* For the moment assume it will be a definition.
  69. * If no compound_statement follows, it is a declaration,
  70. * in which case the class will be adjusted by def2decl().
  71. */
  72. OutDef.od_class = (sc == STATIC ? SFDF : EFDF);
  73. break;
  74. default: /* a variable */
  75. OutDef.od_class =
  76. sc == EXTERN ? EVDC :
  77. sc == STATIC ? SVDF : EVDF;
  78. break;
  79. }
  80. OutDef.od_file = def->df_file;
  81. OutDef.od_line = def->df_line;
  82. OutDef.od_type = (type->tp_fund == FUNCTION ? type->tp_up : type);
  83. OutDef.od_valreturned = NORETURN;
  84. }
  85. def2decl(sc)
  86. int sc;
  87. {
  88. /* It was assumed we were parsing a function definition.
  89. * There was no compound statement following, so actually it was a
  90. * declaration. This function updates the class.
  91. */
  92. OutDef.od_class = (sc == STATIC ? XXDF : EFDC);
  93. }
  94. set_od_valreturned(n)
  95. {
  96. OutDef.od_valreturned = n;
  97. }
  98. local_EFDC(idf)
  99. struct idf *idf;
  100. {
  101. struct outdef od;
  102. od.od_class = EFDC;
  103. od.od_statnr = 0;
  104. od.od_name = idf->id_text;
  105. od.od_file = idf->id_def->df_file;
  106. od.od_line = idf->id_def->df_line;
  107. od.od_type = idf->id_def->df_type->tp_up;
  108. output_def(&od);
  109. /* The other fields are not used for this class. */
  110. }
  111. lint_formals()
  112. {
  113. /* Make a list of tp_entries containing the types of the formal
  114. * parameters of the function definition currently parsed.
  115. */
  116. register struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
  117. register struct tp_entry **hook = &OutDef.od_entry;
  118. OutDef.od_nrargs = 0;
  119. while (se) {
  120. register struct type *type = se->se_idf->id_def->df_type;
  121. register struct tp_entry *te = new_tp_entry();
  122. switch (type->tp_fund) {
  123. /* Do the conversions on the formals that could not be
  124. done in declare_idf().
  125. It is, unfortunately, impossible not to do them,
  126. since the corresponding actuals will have been
  127. converted to generate proper code and we do not
  128. want to duplicate the whole of expression handling
  129. for lint.
  130. */
  131. case CHAR:
  132. case SHORT:
  133. type = int_type;
  134. break;
  135. case FLOAT:
  136. type = double_type;
  137. break;
  138. }
  139. te->te_type = type;
  140. te->te_class = !Const;
  141. *hook = te;
  142. hook = &te->next;
  143. OutDef.od_nrargs++;
  144. se = se->next;
  145. }
  146. if (f_VARARGSn > OutDef.od_nrargs) {
  147. warning("VARARGS%d function has only %d arguments",
  148. f_VARARGSn, OutDef.od_nrargs);
  149. f_VARARGSn = OutDef.od_nrargs;
  150. }
  151. }
  152. output_use(idf)
  153. struct idf *idf;
  154. {
  155. /* Output the usage-definition of the variable described by idf.
  156. */
  157. OutDef.od_name = idf->id_text;
  158. OutDef.od_statnr = (idf->id_def->df_sc == STATIC ? stat_number : 0);
  159. OutDef.od_class = VU;
  160. OutDef.od_file = FileName;
  161. OutDef.od_line = LineNumber;
  162. OutDef.od_type = idf->id_def->df_type;
  163. outdef();
  164. }
  165. outdef()
  166. {
  167. output_def(&OutDef);
  168. }
  169. outcall()
  170. {
  171. output_def(&OutCall);
  172. }
  173. output_def(od)
  174. struct outdef *od;
  175. {
  176. /* As the types are output the tp_entries are removed, because they
  177. * are then not needed anymore.
  178. */
  179. if (od->od_class == XXDF)
  180. return;
  181. if (LINTLIB) {
  182. switch (od->od_class) {
  183. case EFDF:
  184. od->od_class = LFDF;
  185. break;
  186. case EVDF:
  187. od->od_class = LVDF;
  188. break;
  189. case SFDF:
  190. /* remove tp_entries */
  191. while (od->od_entry) {
  192. register struct tp_entry *tmp = od->od_entry;
  193. od->od_entry = od->od_entry->next;
  194. free_tp_entry(tmp);
  195. }
  196. return;
  197. default:
  198. return;
  199. }
  200. }
  201. printf("%s:%d:%c", od->od_name, od->od_statnr, od->od_class);
  202. switch (od->od_class) {
  203. case EFDF:
  204. case SFDF:
  205. case LFDF:
  206. if (f_VARARGSn != -1) {
  207. printf(":%d", -1 - f_VARARGSn);
  208. outtypes(od->od_entry, f_VARARGSn);
  209. }
  210. else {
  211. printf(":%d", od->od_nrargs);
  212. outtypes(od->od_entry, od->od_nrargs);
  213. }
  214. od->od_entry = 0;
  215. printf(":%d", od->od_valreturned);
  216. break;
  217. case FC:
  218. printf(":%d", od->od_nrargs);
  219. outtypes(od->od_entry, od->od_nrargs);
  220. od->od_entry = 0;
  221. printf(":%d", od->od_valused);
  222. break;
  223. case EVDF:
  224. case SVDF:
  225. case LVDF:
  226. case EFDC:
  227. case EVDC:
  228. case IFDC:
  229. case VU:
  230. break;
  231. default:
  232. crash("(output_def) illegal class");
  233. /*NOTREACHED*/
  234. }
  235. printf(":");
  236. outtype(od->od_type);
  237. printf(":%u:%s\n", od->od_line, od->od_file);
  238. }
  239. outtypes(te, n)
  240. struct tp_entry *te;
  241. {
  242. /* Output n types in the tp_entry-list and remove all the entries */
  243. register struct tp_entry *tmp;
  244. while (n--) {
  245. if (!te) {
  246. crash("(outtypes) not enough tp_entries");
  247. /*NOTREACHED*/
  248. }
  249. printf(":");
  250. if (te->te_class == Const && te->te_value >= 0) {
  251. /* constant non-negative actual parameter */
  252. printf("+");
  253. }
  254. outtype(te->te_type);
  255. if (te->te_type->tp_fund == FUNCTION) {
  256. /* UGLY PATCH !!! ??? */
  257. /* function names as operands are sometimes
  258. FUNCTION and sometimes POINTER to FUNCTION,
  259. depending on opaque circumstances. E.g., in
  260. f(main, main);
  261. the first main is PtF and the second is F.
  262. */
  263. printf("*");
  264. }
  265. tmp = te;
  266. te = te->next;
  267. free_tp_entry(tmp);
  268. }
  269. /* remove the remaining entries */
  270. while (te) {
  271. tmp = te;
  272. te = te->next;
  273. free_tp_entry(tmp);
  274. }
  275. }
  276. outtype(tp)
  277. struct type *tp;
  278. {
  279. switch (tp->tp_fund) {
  280. case POINTER:
  281. outtype(tp->tp_up);
  282. printf("*");
  283. break;
  284. case ARRAY:
  285. outtype(tp->tp_up);
  286. printf("*"); /* compatible with [] */
  287. break;
  288. case FUNCTION:
  289. outtype(tp->tp_up);
  290. printf("()");
  291. break;
  292. case STRUCT:
  293. case UNION:
  294. case ENUM:
  295. printf("%s %s", symbol2str(tp->tp_fund), tp->tp_idf->id_text);
  296. break;
  297. case CHAR:
  298. case INT:
  299. case SHORT:
  300. case LONG:
  301. case FLOAT:
  302. case DOUBLE:
  303. case VOID:
  304. case ERRONEOUS:
  305. if (tp->tp_unsigned)
  306. printf("unsigned ");
  307. printf("%s", symbol2str(tp->tp_fund));
  308. break;
  309. default:
  310. crash("(outtype) illegal tp_fund");
  311. /*NOTREACHED*/
  312. }
  313. }
  314. implicit_func_decl(idf, file, line)
  315. struct idf *idf;
  316. char *file;
  317. unsigned int line;
  318. {
  319. struct outdef od;
  320. od.od_class = IFDC;
  321. od.od_statnr = 0;
  322. od.od_name = idf->id_text;
  323. od.od_file = file;
  324. od.od_line = line;
  325. od.od_type = idf->id_def->df_type->tp_up;
  326. output_def(&od);
  327. /* The other fields are not used for this class. */
  328. }
  329. fill_outcall(ex, used)
  330. struct expr *ex;
  331. int used;
  332. {
  333. register struct idf *idf = ex->OP_LEFT->VL_IDF;
  334. register struct def *def = idf->id_def;
  335. if (def->df_sc == IMPLICIT) {
  336. implicit_func_decl(idf, ex->ex_file, ex->ex_line);
  337. }
  338. OutCall.od_type = def->df_type->tp_up;
  339. OutCall.od_statnr = (def->df_sc == STATIC ? stat_number : 0);
  340. OutCall.od_class = FC;
  341. OutCall.od_name = idf->id_text;
  342. OutCall.od_file = ex->ex_file;
  343. OutCall.od_line = ex->ex_line;
  344. OutCall.od_entry = (struct tp_entry *)0;
  345. OutCall.od_nrargs = 0;
  346. if ((ex = ex->OP_RIGHT) != 0) { /* function call with arguments */
  347. /* store types of argument expressions in tp_entries */
  348. while (ex->ex_class == Oper && ex->OP_OPER == PARCOMMA) {
  349. fill_arg(ex->OP_RIGHT);
  350. ex = ex->OP_LEFT;
  351. }
  352. fill_arg(ex);
  353. }
  354. OutCall.od_valused = used; /* USED, IGNORED or VOIDED */
  355. }
  356. fill_arg(e)
  357. struct expr *e;
  358. {
  359. register struct tp_entry *te;
  360. te = new_tp_entry();
  361. te->te_type = e->ex_type;
  362. if (is_cp_cst(e)) {
  363. te->te_class = Const;
  364. te->te_value = e->VL_VALUE;
  365. }
  366. else {
  367. te->te_class = !Const;
  368. te->te_value = (arith) 0;
  369. }
  370. te->next = OutCall.od_entry;
  371. OutCall.od_entry = te;
  372. OutCall.od_nrargs++;
  373. }
  374. #endif LINT