proto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. /* $Id$ */
  6. /* P R O T O T Y P E F I D D L I N G */
  7. #include "lint.h"
  8. #include "debug.h"
  9. #include "idfsize.h"
  10. #include "nparams.h"
  11. #include "botch_free.h"
  12. #include <alloc.h>
  13. #include "Lpars.h"
  14. #include "level.h"
  15. #include <flt_arith.h>
  16. #include "arith.h"
  17. #include "align.h"
  18. #include "stack.h"
  19. #include "idf.h"
  20. #include "def.h"
  21. #include "type.h"
  22. #include "struct.h"
  23. #include "label.h"
  24. #include "expr.h"
  25. #include "declar.h"
  26. #include "decspecs.h"
  27. #include "proto.h"
  28. #include "assert.h"
  29. extern char options[];
  30. check_for_void(pl)
  31. register struct proto *pl;
  32. {
  33. register int errcnt = 0;
  34. if (!pl) return;
  35. if ((pl->pl_flag & PL_VOID) && !(pl->next)) return;
  36. while (pl) {
  37. if (pl->pl_flag & PL_VOID) {
  38. if (!errcnt && !(pl->pl_flag & PL_ERRGIVEN))
  39. error("illegal use of void in argument list");
  40. pl->pl_flag |= PL_ERRGIVEN;
  41. errcnt++;
  42. }
  43. pl = pl->next;
  44. }
  45. }
  46. add_proto(pl, ds, dc, lvl)
  47. struct proto *pl;
  48. struct decspecs *ds;
  49. struct declarator *dc;
  50. int lvl;
  51. {
  52. /* The full typed identifier or abstract type, described
  53. by the structures decspecs and declarator are turned
  54. a into parameter type list structure.
  55. The parameters will be declared at level L_FORMAL2,
  56. later on it's decided whether they were prototypes
  57. or actual declarations.
  58. */
  59. register struct idf *idf = dc->dc_idf;
  60. register struct def *def = idf ? idf->id_def : (struct def *)0;
  61. register int sc = ds->ds_sc;
  62. register struct type *type;
  63. char formal_array = 0;
  64. ASSERT(ds->ds_type != (struct type *)0);
  65. pl->pl_flag = PL_FORMAL;
  66. type = declare_type(ds->ds_type, dc);
  67. if (type->tp_size < (arith)0 && actual_declaration(sc, type)) {
  68. extern char *symbol2str();
  69. if (type->tp_fund != VOID)
  70. error("unknown %s-type", symbol2str(type->tp_fund));
  71. else {
  72. if (idf != (struct idf *)0
  73. || ds->ds_sc_given
  74. || ds->ds_typequal) {
  75. error("illegal use of void in argument list");
  76. pl->pl_flag |= PL_ERRGIVEN;
  77. }
  78. /* set PL_VOID anyway */
  79. pl->pl_flag |= PL_VOID;
  80. }
  81. }
  82. if (ds->ds_sc_given && ds->ds_sc != REGISTER) {
  83. if (!(pl->pl_flag & PL_ERRGIVEN)) {
  84. if (ds->ds_sc != AUTO) {
  85. error("illegal storage class in parameter declaration");
  86. } else {
  87. warning("illegal storage class in parameter declaration");
  88. }
  89. }
  90. }
  91. /* Perform some special conversions for parameters.
  92. */
  93. if (type->tp_fund == FUNCTION) {
  94. type = construct_type(POINTER, type, 0, (arith) 0, NO_PROTO);
  95. } else if (type->tp_fund == ARRAY) {
  96. type = construct_type(POINTER, type->tp_up, 0, (arith) 0, NO_PROTO);
  97. formal_array = 1;
  98. }
  99. /* According to the standard we should ignore the storage
  100. class of a parameter, unless it's part of a function
  101. definition.
  102. However, in the routine declare_protos we don't know decspecs,
  103. and therefore we can't complain up there. So we build up the
  104. storage class, and keep quiet until we reach declare_protos.
  105. */
  106. sc = (ds->ds_sc_given && ds->ds_sc != REGISTER) ?
  107. 0 : sc == 0 ? FORMAL : REGISTER;
  108. if (def && (def->df_level == lvl /* || def->df_level < L_PROTO */ )) {
  109. /* redeclaration at the same level */
  110. error("parameter %s redeclared", idf->id_text);
  111. } else if (idf != (struct idf *)0) {
  112. /* New definition, redefinition hides earlier one
  113. */
  114. register struct def *newdef = new_def();
  115. newdef->next = def;
  116. newdef->df_level = lvl;
  117. newdef->df_sc = sc;
  118. newdef->df_type = type;
  119. newdef->df_formal_array = formal_array;
  120. newdef->df_file = idf->id_file;
  121. newdef->df_line = idf->id_line;
  122. #ifdef LINT
  123. newdef->df_set = (type->tp_fund == ARRAY);
  124. /* newdef->df_firstbrace = 0; */
  125. #endif
  126. /* We can't put the idf onto the stack, since these kinds
  127. of declaration may occurs at any level, and the idf
  128. does not necessarily go at this level. E.g.
  129. f() {
  130. ...
  131. { int func(int a, int b);
  132. ...
  133. }
  134. }
  135. The idf's a and b declared in the prototype declaration
  136. do not go at any level, they are simply ignored.
  137. However, in
  138. f(int a, int b) {
  139. ...
  140. }
  141. They should go at level L_FORMAL2. But at this stage
  142. we don't know whether we have a prototype or function
  143. definition. So, this process is postponed.
  144. */
  145. idf->id_def = newdef;
  146. update_ahead(idf);
  147. }
  148. pl->pl_idf = idf;
  149. pl->pl_type = type;
  150. }
  151. struct tag *
  152. gettag(tp, idpp)
  153. struct type *tp;
  154. struct idf **idpp;
  155. {
  156. struct tag *tg = (struct tag *)0;
  157. register int fund = tp->tp_fund;
  158. while (fund == FIELD || fund == POINTER
  159. || fund == ARRAY || fund == FUNCTION) {
  160. tp = tp->tp_up;
  161. fund = tp->tp_fund;
  162. }
  163. *idpp = tp->tp_idf;
  164. switch(tp->tp_fund) {
  165. case ENUM:
  166. case UNION:
  167. case STRUCT: tg = tp->tp_idf->id_tag; break;
  168. }
  169. return tg;
  170. }
  171. declare_protos(dc)
  172. register struct declarator *dc;
  173. {
  174. /* At this points we know that the idf's in protolist are formal
  175. parameters. So it's time to declare them at level L_FORMAL2.
  176. */
  177. struct stack_level *stl = stack_level_of(L_FORMAL1);
  178. register struct decl_unary *du;
  179. register struct type *type;
  180. register struct proto *pl;
  181. register struct def *def;
  182. #ifdef DEBUG
  183. if (options['t'])
  184. dumpidftab("start declare_protos", 0);
  185. #endif /* DEBUG */
  186. du = dc->dc_decl_unary;
  187. while (du) {
  188. if (du->du_fund == FUNCTION) {
  189. if (du->next != (struct decl_unary *) 0) {
  190. remove_proto_idfs(du->du_proto);
  191. du->du_proto = 0;
  192. } else break;
  193. }
  194. du = du->next;
  195. }
  196. pl = du ? du->du_proto : NO_PROTO;
  197. if (pl) {
  198. #if 0 /* the id_proto member is deleted (???) */
  199. idf->id_proto = 0;
  200. #endif /* 0 */
  201. do {
  202. struct tag *tg;
  203. struct idf *idp = 0;
  204. type = pl->pl_type;
  205. /* `...' only for type checking */
  206. if (pl->pl_flag & PL_ELLIPSIS) {
  207. pl = pl->next;
  208. continue;
  209. }
  210. /* special case: int f(void) { ; } */
  211. if (type->tp_fund == VOID)
  212. break;
  213. if (!pl->pl_idf || !(def = pl->pl_idf->id_def)) {
  214. error("no parameter identifier supplied");
  215. pl = pl->next;
  216. continue;
  217. }
  218. /* Postponed storage class checking.
  219. */
  220. if (def->df_sc == 0)
  221. error("illegal storage class in parameter declaration");
  222. def->df_level = L_FORMAL2;
  223. stack_idf(pl->pl_idf, stl);
  224. pl = pl->next;
  225. tg = gettag(type, &idp);
  226. if (tg && tg->tg_level <= L_PROTO) {
  227. tg->tg_level = L_FORMAL2;
  228. stack_idf(idp, stl);
  229. }
  230. } while (pl);
  231. }
  232. #ifdef DEBUG
  233. if (options['t'])
  234. dumpidftab("end declare_protos", 0);
  235. #endif /* DEBUG */
  236. }
  237. update_proto(tp, otp)
  238. register struct type *tp, *otp;
  239. {
  240. /* This routine performs the proto type updates.
  241. Consider the following code:
  242. int f(double g());
  243. int f(double g(int f(), int));
  244. int f(double g(int f(long double), int));
  245. The most accurate definition is the third line.
  246. This routine will silently update all lists,
  247. and removes the redundant occupied space.
  248. */
  249. register struct proto *pl, *opl;
  250. if (tp == otp) return;
  251. if (!tp || !otp) return;
  252. while (tp->tp_fund != FUNCTION) {
  253. if (tp->tp_fund != POINTER && tp->tp_fund != ARRAY) return;
  254. tp = tp->tp_up;
  255. otp = otp->tp_up;
  256. if (!tp) return;
  257. }
  258. pl = tp->tp_proto;
  259. opl = otp->tp_proto;
  260. if (pl && opl) {
  261. /* both have prototypes */
  262. while (pl && opl) {
  263. update_proto(pl->pl_type, opl->pl_type);
  264. pl = pl->next;
  265. opl = opl->next;
  266. }
  267. /* Do not free the old prototype list. It might be part of
  268. * a typedef.
  269. */
  270. otp->tp_proto = tp->tp_proto;
  271. } else if (opl) {
  272. /* old decl has type */
  273. } else if (pl) {
  274. otp->tp_proto = pl;
  275. }
  276. update_proto(tp->tp_up, otp->tp_up);
  277. }
  278. /* struct/union and enum tags can be declared inside prototypes
  279. * remove them from the symbol-table
  280. */
  281. remove_proto_tag(tp)
  282. struct type *tp;
  283. {
  284. register struct idf *ident;
  285. register struct tag *tgp, **tgpp;
  286. register int fund = tp->tp_fund;
  287. while (fund == FIELD || fund == POINTER
  288. || fund == ARRAY || fund == FUNCTION) {
  289. tp = tp->tp_up;
  290. fund = tp->tp_fund;
  291. }
  292. ident = tp->tp_idf;
  293. switch (tp->tp_fund) {
  294. case ENUM:
  295. case STRUCT:
  296. case UNION: tgpp = &(ident->id_tag); break;
  297. default: return;
  298. }
  299. while((*tgpp) && (*tgpp)->tg_type != tp) {
  300. tgpp = &((*tgpp)->next);
  301. }
  302. if (!*tgpp) return;
  303. tgp = *tgpp;
  304. if (tgp->tg_level > L_PROTO) return;
  305. #ifdef DEBUG
  306. if (options['t'])
  307. print("Removing idf %s from list\n",
  308. ident->id_text);
  309. #endif
  310. (*tgpp) = tgp->next;
  311. free_tag(tgp);
  312. }
  313. remove_proto_idfs(pl)
  314. register struct proto *pl;
  315. {
  316. /* Remove all the identifier definitions from the
  317. prototype list.
  318. */
  319. register struct def *def;
  320. while (pl) {
  321. if (pl->pl_idf) {
  322. #ifdef DEBUG
  323. if (options['t'])
  324. print("Removing idf %s from list\n",
  325. pl->pl_idf->id_text);
  326. #endif
  327. def = pl->pl_idf->id_def;
  328. if (def && def->df_level <= L_PROTO) {
  329. pl->pl_idf->id_def = def->next;
  330. free_def(def);
  331. }
  332. pl->pl_idf = (struct idf *) 0;
  333. }
  334. if (pl->pl_type) {
  335. remove_proto_tag(pl->pl_type);
  336. }
  337. pl = pl->next;
  338. }
  339. }
  340. call_proto(expp)
  341. register struct expr **expp;
  342. {
  343. /* If the function specified by (*expp)->OP_LEFT has a prototype,
  344. the parameters are converted according the rules specified in
  345. par. 3.3.2.2. E.i. the parameters are converted to the prototype
  346. counter parts as if by assignment. For the parameters falling
  347. under ellipsis clause the old parameters conversion stuff
  348. applies.
  349. */
  350. register struct expr *left = (*expp)->OP_LEFT;
  351. register struct expr *right = (*expp)->OP_RIGHT;
  352. register struct proto *pl = NO_PROTO;
  353. static struct proto ellipsis = { 0, 0, 0, PL_ELLIPSIS };
  354. if (left != NILEXPR) { /* in case of an error */
  355. register struct type *tp = left->ex_type;
  356. while (tp && tp->tp_fund != FUNCTION && tp != error_type)
  357. tp = tp->tp_up;
  358. if (tp && tp->tp_proto)
  359. pl = tp->tp_proto;
  360. }
  361. if (right != NILEXPR) { /* function call with parameters */
  362. register struct expr **ep = &((*expp)->OP_RIGHT);
  363. register int ecnt = 0, pcnt = 0;
  364. struct expr **estack[NPARAMS];
  365. struct proto *pstack[NPARAMS];
  366. /* stack up the parameter expressions */
  367. while (right->ex_class == Oper && right->OP_OPER == PARCOMMA) {
  368. if (ecnt == STDC_NPARAMS)
  369. expr_strict(right, "number of parameters exceeds ANSI limit");
  370. if (ecnt >= NPARAMS-1) {
  371. expr_error(right, "too many parameters");
  372. return;
  373. }
  374. estack[ecnt++] = &(right->OP_RIGHT);
  375. ep = &(right->OP_LEFT);
  376. right = right->OP_LEFT;
  377. }
  378. estack[ecnt] = ep;
  379. /* Declarations like int f(void) do not expect any
  380. parameters.
  381. */
  382. if (pl && pl->pl_flag & PL_VOID) {
  383. expr_strict(*expp, "no parameters expected");
  384. pl = NO_PROTO;
  385. }
  386. /* stack up the prototypes */
  387. if (pl) {
  388. pcnt--;
  389. do {
  390. /* stack prototypes */
  391. pstack[++pcnt] = pl;
  392. pl = pl->next;
  393. } while (pl);
  394. }
  395. else {
  396. pstack[0] = &ellipsis;
  397. }
  398. for (ecnt; ecnt >= 0; ecnt--) {
  399. /* Only the parameters specified in the prototype
  400. are checked and converted. The parameters that
  401. fall under the ellipsis clause are neither
  402. checked nor converted !
  403. */
  404. if (pcnt < 0) {
  405. expr_error(*expp, "more parameters than specified in prototype");
  406. break;
  407. }
  408. else if (!(pstack[pcnt]->pl_flag & PL_ELLIPSIS)) {
  409. ch3cast(estack[ecnt],CASTAB,pstack[pcnt]->pl_type);
  410. pcnt--;
  411. } else
  412. any2parameter(estack[ecnt]);
  413. }
  414. if (pcnt > 0 || (pcnt == 0 && !(pstack[0]->pl_flag & PL_ELLIPSIS)))
  415. expr_error(*expp, "fewer parameters than specified in prototype");
  416. } else {
  417. if (pl && !(pl->pl_flag & PL_VOID))
  418. expr_error(*expp, "fewer parameters than specified in prototype");
  419. }
  420. }