proto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 "stack_loc.h"
  20. #include "idf.h"
  21. #include "idf_loc.h"
  22. #include "def.h"
  23. #include "type.h"
  24. #include "ch3.h"
  25. #include "struct.h"
  26. #include "label.h"
  27. #include "expr.h"
  28. #include "declar.h"
  29. #include "decspecs.h"
  30. #include "proto.h"
  31. #include "proto_loc.h"
  32. #include "assert.h"
  33. #include "conversion.h"
  34. #include "error.h"
  35. extern char options[];
  36. void check_for_void(struct proto *pl)
  37. {
  38. int errcnt = 0;
  39. if (!pl) return;
  40. if ((pl->pl_flag & PL_VOID) && !(pl->next)) return;
  41. while (pl) {
  42. if (pl->pl_flag & PL_VOID) {
  43. if (!errcnt && !(pl->pl_flag & PL_ERRGIVEN))
  44. error("illegal use of void in argument list");
  45. pl->pl_flag |= PL_ERRGIVEN;
  46. errcnt++;
  47. }
  48. pl = pl->next;
  49. }
  50. }
  51. void add_proto(struct proto *pl, struct decspecs *ds, struct declarator *dc, 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. struct idf *idf = dc->dc_idf;
  61. struct def *def = idf ? idf->id_def : (struct def *)0;
  62. int sc = ds->ds_sc;
  63. 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. 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. 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. void declare_protos(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. struct decl_unary *du;
  179. struct type *type;
  180. struct proto *pl;
  181. 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. void update_proto(struct type *tp, struct type *otp)
  238. {
  239. /* This routine performs the proto type updates.
  240. Consider the following code:
  241. int f(double g());
  242. int f(double g(int f(), int));
  243. int f(double g(int f(long double), int));
  244. The most accurate definition is the third line.
  245. This routine will silently update all lists,
  246. and removes the redundant occupied space.
  247. */
  248. struct proto *pl, *opl;
  249. if (tp == otp) return;
  250. if (!tp || !otp) return;
  251. while (tp->tp_fund != FUNCTION) {
  252. if (tp->tp_fund != POINTER && tp->tp_fund != ARRAY) return;
  253. tp = tp->tp_up;
  254. otp = otp->tp_up;
  255. if (!tp) return;
  256. }
  257. pl = tp->tp_proto;
  258. opl = otp->tp_proto;
  259. if (pl && opl) {
  260. /* both have prototypes */
  261. while (pl && opl) {
  262. update_proto(pl->pl_type, opl->pl_type);
  263. pl = pl->next;
  264. opl = opl->next;
  265. }
  266. /* Do not free the old prototype list. It might be part of
  267. * a typedef.
  268. */
  269. otp->tp_proto = tp->tp_proto;
  270. } else if (opl) {
  271. /* old decl has type */
  272. } else if (pl) {
  273. otp->tp_proto = pl;
  274. }
  275. update_proto(tp->tp_up, otp->tp_up);
  276. }
  277. /* struct/union and enum tags can be declared inside prototypes
  278. * remove them from the symbol-table
  279. */
  280. void remove_proto_tag(struct type *tp)
  281. {
  282. struct idf *ident;
  283. struct tag *tgp, **tgpp;
  284. int fund = tp->tp_fund;
  285. while (fund == FIELD || fund == POINTER
  286. || fund == ARRAY || fund == FUNCTION) {
  287. tp = tp->tp_up;
  288. fund = tp->tp_fund;
  289. }
  290. ident = tp->tp_idf;
  291. switch (tp->tp_fund) {
  292. case ENUM:
  293. case STRUCT:
  294. case UNION: tgpp = &(ident->id_tag); break;
  295. default: return;
  296. }
  297. while((*tgpp) && (*tgpp)->tg_type != tp) {
  298. tgpp = &((*tgpp)->next);
  299. }
  300. if (!*tgpp) return;
  301. tgp = *tgpp;
  302. if (tgp->tg_level > L_PROTO) return;
  303. #ifdef DEBUG
  304. if (options['t'])
  305. print("Removing idf %s from list\n",
  306. ident->id_text);
  307. #endif
  308. (*tgpp) = tgp->next;
  309. free_tag(tgp);
  310. }
  311. void remove_proto_idfs(struct proto *pl)
  312. {
  313. /* Remove all the identifier definitions from the
  314. prototype list.
  315. */
  316. struct def *def;
  317. while (pl) {
  318. if (pl->pl_idf) {
  319. #ifdef DEBUG
  320. if (options['t'])
  321. print("Removing idf %s from list\n",
  322. pl->pl_idf->id_text);
  323. #endif
  324. def = pl->pl_idf->id_def;
  325. if (def && def->df_level <= L_PROTO) {
  326. pl->pl_idf->id_def = def->next;
  327. free_def(def);
  328. }
  329. pl->pl_idf = (struct idf *) 0;
  330. }
  331. if (pl->pl_type) {
  332. remove_proto_tag(pl->pl_type);
  333. }
  334. pl = pl->next;
  335. }
  336. }
  337. void call_proto(struct expr **expp)
  338. {
  339. /* If the function specified by (*expp)->OP_LEFT has a prototype,
  340. the parameters are converted according the rules specified in
  341. par. 3.3.2.2. E.i. the parameters are converted to the prototype
  342. counter parts as if by assignment. For the parameters falling
  343. under ellipsis clause the old parameters conversion stuff
  344. applies.
  345. */
  346. struct expr *left = (*expp)->OP_LEFT;
  347. struct expr *right = (*expp)->OP_RIGHT;
  348. struct proto *pl = NO_PROTO;
  349. static struct proto ellipsis = { 0, 0, 0, PL_ELLIPSIS };
  350. if (left != NILEXPR) { /* in case of an error */
  351. struct type *tp = left->ex_type;
  352. while (tp && tp->tp_fund != FUNCTION && tp != error_type)
  353. tp = tp->tp_up;
  354. if (tp && tp->tp_proto)
  355. pl = tp->tp_proto;
  356. }
  357. if (right != NILEXPR) { /* function call with parameters */
  358. struct expr **ep = &((*expp)->OP_RIGHT);
  359. int ecnt = 0, pcnt = 0;
  360. struct expr **estack[NPARAMS];
  361. struct proto *pstack[NPARAMS];
  362. /* stack up the parameter expressions */
  363. while (right->ex_class == Oper && right->OP_OPER == PARCOMMA) {
  364. if (ecnt == STDC_NPARAMS)
  365. expr_strict(right, "number of parameters exceeds ANSI limit");
  366. if (ecnt >= NPARAMS-1) {
  367. expr_error(right, "too many parameters");
  368. return;
  369. }
  370. estack[ecnt++] = &(right->OP_RIGHT);
  371. ep = &(right->OP_LEFT);
  372. right = right->OP_LEFT;
  373. }
  374. estack[ecnt] = ep;
  375. /* Declarations like int f(void) do not expect any
  376. parameters.
  377. */
  378. if (pl && pl->pl_flag & PL_VOID) {
  379. expr_strict(*expp, "no parameters expected");
  380. pl = NO_PROTO;
  381. }
  382. /* stack up the prototypes */
  383. if (pl) {
  384. pcnt--;
  385. do {
  386. /* stack prototypes */
  387. pstack[++pcnt] = pl;
  388. pl = pl->next;
  389. } while (pl);
  390. }
  391. else {
  392. pstack[0] = &ellipsis;
  393. }
  394. for (; ecnt >= 0; ecnt--) {
  395. /* Only the parameters specified in the prototype
  396. are checked and converted. The parameters that
  397. fall under the ellipsis clause are neither
  398. checked nor converted !
  399. */
  400. if (pcnt < 0) {
  401. expr_error(*expp, "more parameters than specified in prototype");
  402. break;
  403. }
  404. else if (!(pstack[pcnt]->pl_flag & PL_ELLIPSIS)) {
  405. ch3cast(estack[ecnt],CASTAB,pstack[pcnt]->pl_type);
  406. pcnt--;
  407. } else
  408. any2parameter(estack[ecnt]);
  409. }
  410. if (pcnt > 0 || (pcnt == 0 && !(pstack[0]->pl_flag & PL_ELLIPSIS)))
  411. expr_error(*expp, "fewer parameters than specified in prototype");
  412. } else {
  413. if (pl && !(pl->pl_flag & PL_VOID))
  414. expr_error(*expp, "fewer parameters than specified in prototype");
  415. }
  416. }