proto.c 11 KB

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