proto.c 9.8 KB

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