symbol.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * Symbol lookup and handling.
  3. *
  4. * Copyright (C) 2003 Transmeta Corp.
  5. * 2003-2004 Linus Torvalds
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "lib.h"
  29. #include "allocate.h"
  30. #include "token.h"
  31. #include "parse.h"
  32. #include "symbol.h"
  33. #include "scope.h"
  34. #include "expression.h"
  35. #include "evaluate.h"
  36. #include "target.h"
  37. /*
  38. * Secondary symbol list for stuff that needs to be output because it
  39. * was used.
  40. */
  41. struct symbol_list *translation_unit_used_list = NULL;
  42. /*
  43. * If the symbol is an inline symbol, add it to the list of symbols to parse
  44. */
  45. void access_symbol(struct symbol *sym)
  46. {
  47. if (sym->ctype.modifiers & MOD_INLINE) {
  48. if (!sym->accessed) {
  49. add_symbol(&translation_unit_used_list, sym);
  50. sym->accessed = 1;
  51. }
  52. }
  53. }
  54. struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
  55. {
  56. struct symbol *sym;
  57. for (sym = ident->symbols; sym; sym = sym->next_id) {
  58. if (sym->namespace & ns) {
  59. sym->used = 1;
  60. return sym;
  61. }
  62. }
  63. return NULL;
  64. }
  65. struct context *alloc_context(void)
  66. {
  67. return __alloc_context(0);
  68. }
  69. struct symbol *alloc_symbol(struct position pos, int type)
  70. {
  71. struct symbol *sym = __alloc_symbol(0);
  72. sym->type = type;
  73. sym->pos = pos;
  74. sym->endpos.type = 0;
  75. return sym;
  76. }
  77. struct struct_union_info {
  78. unsigned long max_align;
  79. unsigned long bit_size;
  80. int align_size;
  81. char has_flex_array;
  82. struct symbol *flex_array;
  83. };
  84. /*
  85. * Unions are fairly easy to lay out ;)
  86. */
  87. static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
  88. {
  89. if (sym->bit_size < 0 && is_array_type(sym))
  90. sparse_error(sym->pos, "flexible array member '%s' in a union", show_ident(sym->ident));
  91. if (sym->bit_size > info->bit_size)
  92. info->bit_size = sym->bit_size;
  93. sym->offset = 0;
  94. }
  95. static int bitfield_base_size(struct symbol *sym)
  96. {
  97. if (sym->type == SYM_NODE)
  98. sym = sym->ctype.base_type;
  99. if (sym->type == SYM_BITFIELD)
  100. sym = sym->ctype.base_type;
  101. return sym->bit_size;
  102. }
  103. /*
  104. * Structures are a bit more interesting to lay out
  105. */
  106. static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
  107. {
  108. unsigned long bit_size, align_bit_mask;
  109. int base_size;
  110. bit_size = info->bit_size;
  111. base_size = sym->bit_size;
  112. /*
  113. * If the member is unsized, either it's a flexible array or
  114. * it's invalid and a warning has already been issued.
  115. */
  116. if (base_size < 0) {
  117. if (!is_array_type(sym))
  118. return;
  119. base_size = 0;
  120. info->flex_array = sym;
  121. }
  122. align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
  123. /*
  124. * Bitfields have some very special rules..
  125. */
  126. if (is_bitfield_type (sym)) {
  127. unsigned long bit_offset = bit_size & align_bit_mask;
  128. int room = bitfield_base_size(sym) - bit_offset;
  129. // Zero-width fields just fill up the unit.
  130. int width = base_size ? : (bit_offset ? room : 0);
  131. if (width > room) {
  132. bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
  133. bit_offset = 0;
  134. }
  135. sym->offset = bits_to_bytes(bit_size - bit_offset);
  136. sym->bit_offset = bit_offset;
  137. sym->ctype.base_type->bit_offset = bit_offset;
  138. info->bit_size = bit_size + width;
  139. // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
  140. return;
  141. }
  142. /*
  143. * Otherwise, just align it right and add it up..
  144. */
  145. bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
  146. sym->offset = bits_to_bytes(bit_size);
  147. info->bit_size = bit_size + base_size;
  148. // warning (sym->pos, "regular: offset=%d", sym->offset);
  149. }
  150. static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
  151. {
  152. struct struct_union_info info = {
  153. .max_align = 1,
  154. .bit_size = 0,
  155. .align_size = 1
  156. };
  157. unsigned long bit_size, bit_align;
  158. void (*fn)(struct symbol *, struct struct_union_info *);
  159. struct symbol *member;
  160. fn = advance ? lay_out_struct : lay_out_union;
  161. FOR_EACH_PTR(sym->symbol_list, member) {
  162. if (member->ctype.base_type == &autotype_ctype) {
  163. sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident));
  164. member->ctype.base_type = &incomplete_ctype;
  165. }
  166. if (info.flex_array)
  167. sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident));
  168. examine_symbol_type(member);
  169. if (member->ctype.alignment > info.max_align) {
  170. // Unnamed bitfields do not affect alignment.
  171. if (member->ident || !is_bitfield_type(member))
  172. info.max_align = member->ctype.alignment;
  173. }
  174. if (has_flexible_array(member))
  175. info.has_flex_array = 1;
  176. if (has_flexible_array(member) && Wflexible_array_nested)
  177. warning(member->pos, "nested flexible array");
  178. fn(member, &info);
  179. } END_FOR_EACH_PTR(member);
  180. if (!sym->ctype.alignment)
  181. sym->ctype.alignment = info.max_align;
  182. bit_size = info.bit_size;
  183. if (info.align_size) {
  184. bit_align = bytes_to_bits(sym->ctype.alignment)-1;
  185. bit_size = (bit_size + bit_align) & ~bit_align;
  186. }
  187. if (info.flex_array) {
  188. info.has_flex_array = 1;
  189. }
  190. if (info.has_flex_array && (!is_union_type(sym) || Wflexible_array_union))
  191. sym->has_flex_array = 1;
  192. sym->bit_size = bit_size;
  193. return sym;
  194. }
  195. static struct symbol *examine_base_type(struct symbol *sym)
  196. {
  197. struct symbol *base_type;
  198. if (sym->ctype.base_type == &autotype_ctype) {
  199. struct symbol *type = evaluate_expression(sym->initializer);
  200. if (!type)
  201. type = &bad_ctype;
  202. if (is_bitfield_type(type)) {
  203. warning(sym->pos, "__auto_type on bitfield");
  204. if (type->type == SYM_NODE)
  205. type = type->ctype.base_type;
  206. type = type->ctype.base_type;
  207. }
  208. sym->ctype.base_type = type;
  209. }
  210. /* Check the base type */
  211. base_type = examine_symbol_type(sym->ctype.base_type);
  212. if (!base_type || base_type->type == SYM_PTR)
  213. return base_type;
  214. combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
  215. sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
  216. concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
  217. (struct ptr_list **)&sym->ctype.contexts);
  218. if (base_type->type == SYM_NODE) {
  219. base_type = base_type->ctype.base_type;
  220. sym->ctype.base_type = base_type;
  221. sym->rank = base_type->rank;
  222. }
  223. return base_type;
  224. }
  225. static struct symbol * examine_array_type(struct symbol *sym)
  226. {
  227. struct symbol *base_type = examine_base_type(sym);
  228. unsigned long bit_size = -1, alignment;
  229. struct expression *array_size = sym->array_size;
  230. if (!base_type)
  231. return sym;
  232. if (array_size) {
  233. bit_size = array_element_offset(base_type->bit_size,
  234. get_expression_value_silent(array_size));
  235. if (array_size->type != EXPR_VALUE) {
  236. if (Wvla)
  237. warning(array_size->pos, "Variable length array is used.");
  238. bit_size = -1;
  239. }
  240. }
  241. if (has_flexible_array(base_type) && Wflexible_array_array)
  242. warning(sym->pos, "array of flexible structures");
  243. alignment = base_type->ctype.alignment;
  244. if (!sym->ctype.alignment)
  245. sym->ctype.alignment = alignment;
  246. sym->bit_size = bit_size;
  247. return sym;
  248. }
  249. static struct symbol *examine_bitfield_type(struct symbol *sym)
  250. {
  251. struct symbol *base_type = examine_base_type(sym);
  252. unsigned long alignment, modifiers;
  253. if (!base_type)
  254. return sym;
  255. if (sym->bit_size > base_type->bit_size) {
  256. sparse_error(sym->pos, "bitfield '%s' is wider (%d) than its type (%s)",
  257. show_ident(sym->ident), sym->bit_size, show_typename(base_type));
  258. sym->bit_size = -1;
  259. }
  260. alignment = base_type->ctype.alignment;
  261. if (!sym->ctype.alignment)
  262. sym->ctype.alignment = alignment;
  263. modifiers = base_type->ctype.modifiers;
  264. /* Bitfields are unsigned, unless the base type was explicitly signed */
  265. if (!(modifiers & MOD_EXPLICITLY_SIGNED))
  266. modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
  267. sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
  268. return sym;
  269. }
  270. /*
  271. * "typeof" will have to merge the types together
  272. */
  273. void merge_type(struct symbol *sym, struct symbol *base_type)
  274. {
  275. combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
  276. sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
  277. concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
  278. (struct ptr_list **)&sym->ctype.contexts);
  279. sym->ctype.base_type = base_type->ctype.base_type;
  280. if (sym->ctype.base_type->type == SYM_NODE)
  281. merge_type(sym, sym->ctype.base_type);
  282. }
  283. static bool is_wstring_expr(struct expression *expr)
  284. {
  285. while (expr) {
  286. switch (expr->type) {
  287. case EXPR_STRING:
  288. return 1;
  289. case EXPR_INITIALIZER:
  290. if (expression_list_size(expr->expr_list) != 1)
  291. return 0;
  292. expr = first_expression(expr->expr_list);
  293. break;
  294. case EXPR_PREOP:
  295. if (expr->op == '(') {
  296. expr = expr->unop;
  297. break;
  298. }
  299. default:
  300. return 0;
  301. }
  302. }
  303. return 0;
  304. }
  305. static int count_array_initializer(struct symbol *t, struct expression *expr)
  306. {
  307. int nr = 0;
  308. int is_char = 0;
  309. /*
  310. * Arrays of character types are special; they can be initialized by
  311. * string literal _or_ by string literal in braces. The latter means
  312. * that with T x[] = {<string literal>} number of elements in x depends
  313. * on T - if it's a character type, we get the length of string literal
  314. * (including NUL), otherwise we have one element here.
  315. */
  316. if (t->ctype.base_type == &int_type && t->rank == -2)
  317. is_char = 1;
  318. else if (t == wchar_ctype && is_wstring_expr(expr))
  319. is_char = 1;
  320. switch (expr->type) {
  321. case EXPR_INITIALIZER: {
  322. struct expression *entry;
  323. int count = 0;
  324. int str_len = 0;
  325. FOR_EACH_PTR(expr->expr_list, entry) {
  326. count++;
  327. switch (entry->type) {
  328. case EXPR_INDEX:
  329. if (entry->idx_to >= nr)
  330. nr = entry->idx_to+1;
  331. break;
  332. case EXPR_PREOP: {
  333. struct expression *e = entry;
  334. if (is_char) {
  335. while (e && e->type == EXPR_PREOP && e->op == '(')
  336. e = e->unop;
  337. if (e && e->type == EXPR_STRING) {
  338. entry = e;
  339. case EXPR_STRING:
  340. if (is_char)
  341. str_len = entry->string->length;
  342. }
  343. }
  344. }
  345. default:
  346. nr++;
  347. }
  348. } END_FOR_EACH_PTR(entry);
  349. if (count == 1 && str_len)
  350. nr = str_len;
  351. break;
  352. }
  353. case EXPR_PREOP:
  354. if (is_char) {
  355. struct expression *e = expr;
  356. while (e && e->type == EXPR_PREOP && e->op == '(')
  357. e = e->unop;
  358. if (e && e->type == EXPR_STRING) {
  359. expr = e;
  360. case EXPR_STRING:
  361. if (is_char)
  362. nr = expr->string->length;
  363. }
  364. }
  365. break;
  366. default:
  367. break;
  368. }
  369. return nr;
  370. }
  371. static struct expression *get_symbol_initializer(struct symbol *sym)
  372. {
  373. do {
  374. if (sym->initializer)
  375. return sym->initializer;
  376. } while ((sym = sym->same_symbol) != NULL);
  377. return NULL;
  378. }
  379. static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
  380. {
  381. struct symbol *arr_ori = node->ctype.base_type;
  382. struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
  383. struct symbol *elem_type = arr_ori->ctype.base_type;
  384. struct expression *size = alloc_const_expression(node->pos, count);
  385. unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
  386. *arr_new = *arr_ori;
  387. arr_new->bit_size = bit_size;
  388. arr_new->array_size = size;
  389. node->array_size = size;
  390. node->ctype.base_type = arr_new;
  391. return bit_size;
  392. }
  393. static struct symbol * examine_node_type(struct symbol *sym)
  394. {
  395. struct symbol *base_type = examine_base_type(sym);
  396. int bit_size;
  397. unsigned long alignment;
  398. /* SYM_NODE - figure out what the type of the node was.. */
  399. bit_size = 0;
  400. alignment = 0;
  401. if (!base_type)
  402. return sym;
  403. bit_size = base_type->bit_size;
  404. alignment = base_type->ctype.alignment;
  405. /* Pick up signedness information into the node */
  406. sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
  407. if (!sym->ctype.alignment)
  408. sym->ctype.alignment = alignment;
  409. /* Unsized array? The size might come from the initializer.. */
  410. if (bit_size < 0 && base_type->type == SYM_ARRAY) {
  411. struct expression *initializer = get_symbol_initializer(sym);
  412. if (initializer) {
  413. struct symbol *node_type = base_type->ctype.base_type;
  414. int count = count_array_initializer(node_type, initializer);
  415. if (node_type && node_type->bit_size >= 0)
  416. bit_size = implicit_array_size(sym, count);
  417. }
  418. }
  419. sym->bit_size = bit_size;
  420. sym->rank = base_type->rank;
  421. return sym;
  422. }
  423. static struct symbol *examine_enum_type(struct symbol *sym)
  424. {
  425. struct symbol *base_type = examine_base_type(sym);
  426. sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
  427. sym->bit_size = bits_in_enum;
  428. if (base_type->bit_size > sym->bit_size)
  429. sym->bit_size = base_type->bit_size;
  430. sym->ctype.alignment = enum_alignment;
  431. if (base_type->ctype.alignment > sym->ctype.alignment)
  432. sym->ctype.alignment = base_type->ctype.alignment;
  433. return sym;
  434. }
  435. static struct symbol *examine_pointer_type(struct symbol *sym)
  436. {
  437. /*
  438. * Since pointers to incomplete types can be used,
  439. * for example in a struct-declaration-list,
  440. * the base type must *not* be examined here.
  441. * It thus means that it needs to be done later,
  442. * when the base type of the pointer is looked at.
  443. */
  444. if (!sym->bit_size)
  445. sym->bit_size = bits_in_pointer;
  446. if (!sym->ctype.alignment)
  447. sym->ctype.alignment = pointer_alignment;
  448. return sym;
  449. }
  450. static struct symbol *examine_typeof(struct symbol *sym)
  451. {
  452. struct symbol *base = evaluate_expression(sym->initializer);
  453. unsigned long mod = 0;
  454. if (!base)
  455. base = &bad_ctype;
  456. if (base->type == SYM_NODE) {
  457. mod |= base->ctype.modifiers & MOD_TYPEOF;
  458. base = base->ctype.base_type;
  459. }
  460. if (base->type == SYM_BITFIELD)
  461. warning(base->pos, "typeof applied to bitfield type");
  462. sym->type = SYM_NODE;
  463. sym->ctype.modifiers = mod;
  464. sym->ctype.base_type = base;
  465. return examine_node_type(sym);
  466. }
  467. /*
  468. * Fill in type size and alignment information for
  469. * regular SYM_TYPE things.
  470. */
  471. struct symbol *examine_symbol_type(struct symbol * sym)
  472. {
  473. if (!sym)
  474. return sym;
  475. /* Already done? */
  476. if (sym->examined)
  477. return sym;
  478. sym->examined = 1;
  479. switch (sym->type) {
  480. case SYM_FN:
  481. case SYM_NODE:
  482. return examine_node_type(sym);
  483. case SYM_ARRAY:
  484. return examine_array_type(sym);
  485. case SYM_STRUCT:
  486. return examine_struct_union_type(sym, 1);
  487. case SYM_UNION:
  488. return examine_struct_union_type(sym, 0);
  489. case SYM_PTR:
  490. return examine_pointer_type(sym);
  491. case SYM_ENUM:
  492. return examine_enum_type(sym);
  493. case SYM_BITFIELD:
  494. return examine_bitfield_type(sym);
  495. case SYM_BASETYPE:
  496. /* Size and alignment had better already be set up */
  497. return sym;
  498. case SYM_TYPEOF:
  499. return examine_typeof(sym);
  500. case SYM_PREPROCESSOR:
  501. sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
  502. return NULL;
  503. case SYM_UNINITIALIZED:
  504. sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
  505. return NULL;
  506. case SYM_RESTRICT:
  507. examine_base_type(sym);
  508. return sym;
  509. case SYM_FOULED:
  510. examine_base_type(sym);
  511. return sym;
  512. default:
  513. sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
  514. break;
  515. }
  516. return sym;
  517. }
  518. const char* get_type_name(enum type type)
  519. {
  520. const char *type_lookup[] = {
  521. [SYM_UNINITIALIZED] = "uninitialized",
  522. [SYM_PREPROCESSOR] = "preprocessor",
  523. [SYM_BASETYPE] = "basetype",
  524. [SYM_NODE] = "node",
  525. [SYM_PTR] = "pointer",
  526. [SYM_FN] = "function",
  527. [SYM_ARRAY] = "array",
  528. [SYM_STRUCT] = "struct",
  529. [SYM_UNION] = "union",
  530. [SYM_ENUM] = "enum",
  531. [SYM_TYPEOF] = "typeof",
  532. [SYM_BITFIELD] = "bitfield",
  533. [SYM_LABEL] = "label",
  534. [SYM_RESTRICT] = "restrict",
  535. [SYM_FOULED] = "fouled",
  536. [SYM_KEYWORD] = "keyword",
  537. [SYM_BAD] = "bad"};
  538. if (type <= SYM_BAD)
  539. return type_lookup[type];
  540. else
  541. return NULL;
  542. }
  543. struct symbol *examine_pointer_target(struct symbol *sym)
  544. {
  545. return examine_base_type(sym);
  546. }
  547. static struct symbol_list *restr, *fouled;
  548. void create_fouled(struct symbol *type)
  549. {
  550. if (type->bit_size < bits_in_int) {
  551. struct symbol *new = alloc_symbol(type->pos, type->type);
  552. *new = *type;
  553. new->bit_size = bits_in_int;
  554. new->rank = 0;
  555. new->type = SYM_FOULED;
  556. new->ctype.base_type = type;
  557. add_symbol(&restr, type);
  558. add_symbol(&fouled, new);
  559. }
  560. }
  561. struct symbol *befoul(struct symbol *type)
  562. {
  563. struct symbol *t1, *t2;
  564. while (type->type == SYM_NODE)
  565. type = type->ctype.base_type;
  566. PREPARE_PTR_LIST(restr, t1);
  567. PREPARE_PTR_LIST(fouled, t2);
  568. for (;;) {
  569. if (t1 == type)
  570. return t2;
  571. if (!t1)
  572. break;
  573. NEXT_PTR_LIST(t1);
  574. NEXT_PTR_LIST(t2);
  575. }
  576. FINISH_PTR_LIST(t2);
  577. FINISH_PTR_LIST(t1);
  578. return NULL;
  579. }
  580. static void inherit_declaration(struct symbol *sym, struct symbol *prev)
  581. {
  582. unsigned long mods = prev->ctype.modifiers;
  583. // inherit function attributes
  584. sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
  585. }
  586. void check_declaration(struct symbol *sym)
  587. {
  588. int warned = 0;
  589. struct symbol *next = sym;
  590. while ((next = next->next_id) != NULL) {
  591. if (next->namespace != sym->namespace)
  592. continue;
  593. if (sym->scope == next->scope) {
  594. sym->same_symbol = next;
  595. inherit_declaration(sym, next);
  596. return;
  597. }
  598. /* Extern in block level matches a TOPLEVEL non-static symbol */
  599. if (sym->ctype.modifiers & MOD_EXTERN) {
  600. if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
  601. sym->same_symbol = next;
  602. return;
  603. }
  604. }
  605. if (!Wshadow || warned)
  606. continue;
  607. if (get_sym_type(next) == SYM_FN)
  608. continue;
  609. warned = 1;
  610. warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
  611. info(next->pos, "originally declared here");
  612. }
  613. }
  614. static void inherit_static(struct symbol *sym)
  615. {
  616. struct symbol *prev;
  617. // only 'plain' symbols are concerned
  618. if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
  619. return;
  620. for (prev = sym->next_id; prev; prev = prev->next_id) {
  621. if (prev->namespace != NS_SYMBOL)
  622. continue;
  623. if (prev->scope != file_scope)
  624. continue;
  625. sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
  626. // previous declarations are already converted
  627. return;
  628. }
  629. }
  630. void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
  631. {
  632. if (sym->bound) {
  633. sparse_error(sym->pos, "internal error: symbol type already bound");
  634. return;
  635. }
  636. if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
  637. sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
  638. return;
  639. }
  640. sym->namespace = ns;
  641. sym->next_id = ident->symbols;
  642. ident->symbols = sym;
  643. if (sym->ident && sym->ident != ident)
  644. warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
  645. sym->ident = ident;
  646. sym->bound = 1;
  647. if (ns == NS_SYMBOL && toplevel(scope)) {
  648. unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
  649. inherit_static(sym);
  650. scope = global_scope;
  651. if (sym->ctype.modifiers & MOD_STATIC ||
  652. is_extern_inline(sym)) {
  653. scope = file_scope;
  654. mod = MOD_TOPLEVEL;
  655. }
  656. sym->ctype.modifiers |= mod;
  657. }
  658. bind_scope(sym, scope);
  659. }
  660. void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
  661. {
  662. struct scope *scope = block_scope;;
  663. if (ns == NS_MACRO)
  664. scope = file_scope;
  665. if (ns == NS_LABEL)
  666. scope = function_scope;
  667. bind_symbol_with_scope(sym, ident, ns, scope);
  668. }
  669. struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
  670. {
  671. struct ident *ident = built_in_ident(name);
  672. struct symbol *sym = lookup_symbol(ident, namespace);
  673. if (sym && sym->type != type)
  674. die("symbol %s created with different types: %d old %d", name,
  675. type, sym->type);
  676. if (!sym) {
  677. struct token *token = built_in_token(stream, ident);
  678. sym = alloc_symbol(token->pos, type);
  679. bind_symbol(sym, token->ident, namespace);
  680. }
  681. return sym;
  682. }
  683. /*
  684. * Abstract types
  685. */
  686. struct symbol int_type,
  687. fp_type;
  688. /*
  689. * C types (i.e. actual instances that the abstract types
  690. * can map onto)
  691. */
  692. struct symbol bool_ctype, void_ctype, type_ctype,
  693. char_ctype, schar_ctype, uchar_ctype,
  694. short_ctype, sshort_ctype, ushort_ctype,
  695. int_ctype, sint_ctype, uint_ctype,
  696. long_ctype, slong_ctype, ulong_ctype,
  697. llong_ctype, sllong_ctype, ullong_ctype,
  698. int128_ctype, sint128_ctype, uint128_ctype,
  699. float_ctype, double_ctype, ldouble_ctype,
  700. string_ctype, ptr_ctype, lazy_ptr_ctype,
  701. incomplete_ctype, label_ctype, bad_ctype,
  702. null_ctype;
  703. struct symbol autotype_ctype;
  704. struct symbol schar_ptr_ctype, short_ptr_ctype;
  705. struct symbol int_ptr_ctype, uint_ptr_ctype;
  706. struct symbol long_ptr_ctype, ulong_ptr_ctype;
  707. struct symbol llong_ptr_ctype, ullong_ptr_ctype;
  708. struct symbol size_t_ptr_ctype, intmax_ptr_ctype, ptrdiff_ptr_ctype;
  709. struct symbol float32_ctype, float32x_ctype;
  710. struct symbol float64_ctype, float64x_ctype;
  711. struct symbol float128_ctype;
  712. struct symbol const_void_ctype, const_char_ctype;
  713. struct symbol const_ptr_ctype, const_string_ctype;
  714. struct symbol const_wchar_ctype, const_wstring_ctype;
  715. struct symbol zero_int;
  716. #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
  717. #define __IDENT(n,str,res) \
  718. struct ident n = __INIT_IDENT(str,res)
  719. #include "ident-list.h"
  720. void init_symbols(void)
  721. {
  722. int stream = init_stream(NULL, "builtin", -1, includepath);
  723. #define __IDENT(n,str,res) \
  724. hash_ident(&n)
  725. #include "ident-list.h"
  726. init_parser(stream);
  727. }
  728. // For fix-sized types
  729. static int bits_in_type32 = 32;
  730. static int bits_in_type64 = 64;
  731. static int bits_in_type128 = 128;
  732. #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
  733. #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
  734. #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
  735. #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
  736. #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
  737. #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
  738. #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
  739. #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
  740. #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
  741. #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
  742. static const struct ctype_declare {
  743. struct symbol *ptr;
  744. enum type type;
  745. unsigned long modifiers;
  746. int rank;
  747. int *bit_size;
  748. int *maxalign;
  749. struct symbol *base_type;
  750. } ctype_declaration[] = {
  751. { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
  752. { &void_ctype, T_BASETYPE },
  753. { &type_ctype, T_BASETYPE },
  754. { &incomplete_ctype, T_BASETYPE },
  755. { &autotype_ctype, T_BASETYPE },
  756. { &bad_ctype, T_BASETYPE },
  757. { &char_ctype, T__INT(-2, char) },
  758. { &schar_ctype, T_SINT(-2, char) },
  759. { &uchar_ctype, T_UINT(-2, char) },
  760. { &short_ctype, T__INT(-1, short) },
  761. { &sshort_ctype, T_SINT(-1, short) },
  762. { &ushort_ctype, T_UINT(-1, short) },
  763. { &int_ctype, T__INT( 0, int) },
  764. { &sint_ctype, T_SINT( 0, int) },
  765. { &uint_ctype, T_UINT( 0, int) },
  766. { &long_ctype, T__INT( 1, long) },
  767. { &slong_ctype, T_SINT( 1, long) },
  768. { &ulong_ctype, T_UINT( 1, long) },
  769. { &llong_ctype, T__INT( 2, longlong) },
  770. { &sllong_ctype, T_SINT( 2, longlong) },
  771. { &ullong_ctype, T_UINT( 2, longlong) },
  772. { &int128_ctype, T__INT( 3, type128) },
  773. { &sint128_ctype, T_SINT( 3, type128) },
  774. { &uint128_ctype, T_UINT( 3, type128) },
  775. { &float_ctype, T_FLOAT(-1, float) },
  776. { &double_ctype, T_FLOAT( 0, double) },
  777. { &ldouble_ctype, T_FLOAT( 1, longdouble) },
  778. { &float32_ctype, T_FLOAT(-1, type32) },
  779. { &float32x_ctype, T_FLOAT(-1, double) },
  780. { &float64_ctype, T_FLOAT( 0, type64) },
  781. { &float64x_ctype, T_FLOAT( 1, longdouble) },
  782. { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
  783. { &string_ctype, T_PTR(&char_ctype) },
  784. { &ptr_ctype, T_PTR(&void_ctype) },
  785. { &null_ctype, T_PTR(&void_ctype) },
  786. { &label_ctype, T_PTR(&void_ctype) },
  787. { &lazy_ptr_ctype, T_PTR(&void_ctype) },
  788. { &schar_ptr_ctype, T_PTR(&schar_ctype) },
  789. { &short_ptr_ctype, T_PTR(&short_ctype) },
  790. { &int_ptr_ctype, T_PTR(&int_ctype) },
  791. { &uint_ptr_ctype, T_PTR(&uint_ctype) },
  792. { &long_ptr_ctype, T_PTR(&long_ctype) },
  793. { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
  794. { &llong_ptr_ctype, T_PTR(&llong_ctype) },
  795. { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
  796. { &size_t_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
  797. { &intmax_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
  798. { &ptrdiff_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
  799. { &const_ptr_ctype, T_PTR(&const_void_ctype) },
  800. { &const_string_ctype, T_PTR(&const_char_ctype) },
  801. { &const_wstring_ctype,T_PTR(&const_wchar_ctype) },
  802. { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
  803. { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
  804. { &const_wchar_ctype, T_CONST(&int_ctype, NULL, NULL) },
  805. { NULL, }
  806. };
  807. void init_ctype(void)
  808. {
  809. const struct ctype_declare *ctype;
  810. for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
  811. struct symbol *sym = ctype->ptr;
  812. unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
  813. unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
  814. unsigned long alignment = bits_to_bytes(bit_size);
  815. if (alignment > maxalign)
  816. alignment = maxalign;
  817. sym->type = ctype->type;
  818. sym->rank = ctype->rank;
  819. sym->bit_size = bit_size;
  820. sym->ctype.alignment = alignment;
  821. sym->ctype.base_type = ctype->base_type;
  822. sym->ctype.modifiers = ctype->modifiers;
  823. if (sym->type == SYM_NODE) {
  824. struct symbol *base = sym->ctype.base_type;
  825. sym->rank = base->rank;
  826. if (!ctype->bit_size)
  827. sym->bit_size = base->bit_size;
  828. if (!ctype->maxalign)
  829. sym->ctype.alignment = base->ctype.alignment;
  830. }
  831. }
  832. // and now some adjustments
  833. if (funsigned_char) {
  834. char_ctype.ctype.modifiers |= MOD_UNSIGNED;
  835. char_ctype.ctype.modifiers &= ~MOD_SIGNED;
  836. }
  837. if (!ptrdiff_ctype)
  838. ptrdiff_ctype = ssize_t_ctype;
  839. if (!intptr_ctype)
  840. intptr_ctype = ssize_t_ctype;
  841. if (!uintptr_ctype)
  842. uintptr_ctype = size_t_ctype;
  843. size_t_ptr_ctype.ctype.base_type = size_t_ctype;
  844. intmax_ptr_ctype.ctype.base_type = intmax_ctype;
  845. ptrdiff_ptr_ctype.ctype.base_type = ptrdiff_ctype;
  846. const_wchar_ctype.ctype.base_type = wchar_ctype;
  847. const_wchar_ctype.rank = wchar_ctype->rank;
  848. const_wchar_ctype.ctype.alignment = wchar_ctype->ctype.alignment;
  849. const_wchar_ctype.bit_size = wchar_ctype->bit_size;
  850. }