PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/d/dmd2/init.c

https://bitbucket.org/goshawk/gdc/
C | 895 lines | 715 code | 108 blank | 72 comment | 154 complexity | 6f7eef245376c0266bde011cfb6346b7 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. // Compiler implementation of the D programming language
  2. // Copyright (c) 1999-2011 by Digital Mars
  3. // All Rights Reserved
  4. // written by Walter Bright
  5. // http://www.digitalmars.com
  6. // License for redistribution is by either the Artistic License
  7. // in artistic.txt, or the GNU General Public License in gnu.txt.
  8. // See the included readme.txt for details.
  9. /* NOTE: This file has been patched from the original DMD distribution to
  10. work with the GDC compiler.
  11. Modified by David Friedman, December 2006
  12. */
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #include "mars.h"
  16. #include "init.h"
  17. #include "expression.h"
  18. #include "statement.h"
  19. #include "identifier.h"
  20. #include "declaration.h"
  21. #include "aggregate.h"
  22. #include "scope.h"
  23. #include "mtype.h"
  24. #include "hdrgen.h"
  25. /********************************** Initializer *******************************/
  26. Initializer::Initializer(Loc loc)
  27. {
  28. this->loc = loc;
  29. }
  30. Initializer *Initializer::syntaxCopy()
  31. {
  32. return this;
  33. }
  34. Initializer *Initializer::semantic(Scope *sc, Type *t, int needInterpret)
  35. {
  36. return this;
  37. }
  38. Type *Initializer::inferType(Scope *sc)
  39. {
  40. error(loc, "cannot infer type from initializer");
  41. return Type::terror;
  42. }
  43. Initializers *Initializer::arraySyntaxCopy(Initializers *ai)
  44. { Initializers *a = NULL;
  45. if (ai)
  46. {
  47. a = new Initializers();
  48. a->setDim(ai->dim);
  49. for (size_t i = 0; i < a->dim; i++)
  50. { Initializer *e = ai->tdata()[i];
  51. e = e->syntaxCopy();
  52. a->tdata()[i] = e;
  53. }
  54. }
  55. return a;
  56. }
  57. char *Initializer::toChars()
  58. { OutBuffer *buf;
  59. HdrGenState hgs;
  60. memset(&hgs, 0, sizeof(hgs));
  61. buf = new OutBuffer();
  62. toCBuffer(buf, &hgs);
  63. return buf->toChars();
  64. }
  65. /********************************** VoidInitializer ***************************/
  66. VoidInitializer::VoidInitializer(Loc loc)
  67. : Initializer(loc)
  68. {
  69. type = NULL;
  70. }
  71. Initializer *VoidInitializer::syntaxCopy()
  72. {
  73. return new VoidInitializer(loc);
  74. }
  75. Initializer *VoidInitializer::semantic(Scope *sc, Type *t, int needInterpret)
  76. {
  77. //printf("VoidInitializer::semantic(t = %p)\n", t);
  78. type = t;
  79. return this;
  80. }
  81. Expression *VoidInitializer::toExpression()
  82. {
  83. error(loc, "void initializer has no value");
  84. return new IntegerExp(0);
  85. }
  86. void VoidInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  87. {
  88. buf->writestring("void");
  89. }
  90. /********************************** StructInitializer *************************/
  91. StructInitializer::StructInitializer(Loc loc)
  92. : Initializer(loc)
  93. {
  94. ad = NULL;
  95. }
  96. Initializer *StructInitializer::syntaxCopy()
  97. {
  98. StructInitializer *ai = new StructInitializer(loc);
  99. assert(field.dim == value.dim);
  100. ai->field.setDim(field.dim);
  101. ai->value.setDim(value.dim);
  102. for (size_t i = 0; i < field.dim; i++)
  103. {
  104. ai->field.tdata()[i] = field.tdata()[i];
  105. Initializer *init = value.tdata()[i];
  106. init = init->syntaxCopy();
  107. ai->value.tdata()[i] = init;
  108. }
  109. return ai;
  110. }
  111. void StructInitializer::addInit(Identifier *field, Initializer *value)
  112. {
  113. //printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
  114. this->field.push(field);
  115. this->value.push(value);
  116. }
  117. Initializer *StructInitializer::semantic(Scope *sc, Type *t, int needInterpret)
  118. {
  119. int errors = 0;
  120. //printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
  121. vars.setDim(field.dim);
  122. t = t->toBasetype();
  123. if (t->ty == Tstruct)
  124. {
  125. unsigned fieldi = 0;
  126. TypeStruct *ts = (TypeStruct *)t;
  127. ad = ts->sym;
  128. if (ad->ctor)
  129. error(loc, "%s %s has constructors, cannot use { initializers }, use %s( initializers ) instead",
  130. ad->kind(), ad->toChars(), ad->toChars());
  131. size_t nfields = ad->fields.dim;
  132. if (((StructDeclaration *)ad)->isnested) nfields--;
  133. for (size_t i = 0; i < field.dim; i++)
  134. {
  135. Identifier *id = field.tdata()[i];
  136. Initializer *val = value.tdata()[i];
  137. Dsymbol *s;
  138. VarDeclaration *v;
  139. if (id == NULL)
  140. {
  141. if (fieldi >= nfields)
  142. { error(loc, "too many initializers for %s", ad->toChars());
  143. errors = 1;
  144. field.remove(i);
  145. i--;
  146. continue;
  147. }
  148. else
  149. {
  150. s = ad->fields.tdata()[fieldi];
  151. }
  152. }
  153. else
  154. {
  155. //s = ad->symtab->lookup(id);
  156. s = ad->search(loc, id, 0);
  157. if (!s)
  158. {
  159. error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
  160. errors = 1;
  161. continue;
  162. }
  163. // Find out which field index it is
  164. for (fieldi = 0; 1; fieldi++)
  165. {
  166. if (fieldi >= nfields)
  167. {
  168. error(loc, "%s.%s is not a per-instance initializable field",
  169. t->toChars(), s->toChars());
  170. errors = 1;
  171. break;
  172. }
  173. if (s == ad->fields.tdata()[fieldi])
  174. break;
  175. }
  176. }
  177. if (s && (v = s->isVarDeclaration()) != NULL)
  178. {
  179. val = val->semantic(sc, v->type, needInterpret);
  180. value.tdata()[i] = val;
  181. vars.tdata()[i] = v;
  182. }
  183. else
  184. { error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
  185. errors = 1;
  186. }
  187. fieldi++;
  188. }
  189. }
  190. else if (t->ty == Tdelegate && value.dim == 0)
  191. { /* Rewrite as empty delegate literal { }
  192. */
  193. Parameters *arguments = new Parameters;
  194. Type *tf = new TypeFunction(arguments, NULL, 0, LINKd);
  195. FuncLiteralDeclaration *fd = new FuncLiteralDeclaration(loc, 0, tf, TOKdelegate, NULL);
  196. fd->fbody = new CompoundStatement(loc, new Statements());
  197. fd->endloc = loc;
  198. Expression *e = new FuncExp(loc, fd);
  199. ExpInitializer *ie = new ExpInitializer(loc, e);
  200. return ie->semantic(sc, t, needInterpret);
  201. }
  202. else
  203. {
  204. error(loc, "a struct is not a valid initializer for a %s", t->toChars());
  205. errors = 1;
  206. }
  207. if (errors)
  208. {
  209. field.setDim(0);
  210. value.setDim(0);
  211. vars.setDim(0);
  212. }
  213. return this;
  214. }
  215. /***************************************
  216. * This works by transforming a struct initializer into
  217. * a struct literal. In the future, the two should be the
  218. * same thing.
  219. */
  220. Expression *StructInitializer::toExpression()
  221. { Expression *e;
  222. size_t offset;
  223. //printf("StructInitializer::toExpression() %s\n", toChars());
  224. if (!ad) // if fwd referenced
  225. {
  226. return NULL;
  227. }
  228. StructDeclaration *sd = ad->isStructDeclaration();
  229. if (!sd)
  230. return NULL;
  231. Expressions *elements = new Expressions();
  232. size_t nfields = ad->fields.dim;
  233. #if DMDV2
  234. if (sd->isnested)
  235. nfields--;
  236. #endif
  237. elements->setDim(nfields);
  238. for (size_t i = 0; i < elements->dim; i++)
  239. {
  240. elements->tdata()[i] = NULL;
  241. }
  242. unsigned fieldi = 0;
  243. for (size_t i = 0; i < value.dim; i++)
  244. {
  245. Identifier *id = field.tdata()[i];
  246. if (id)
  247. {
  248. Dsymbol * s = ad->search(loc, id, 0);
  249. if (!s)
  250. {
  251. error(loc, "'%s' is not a member of '%s'", id->toChars(), sd->toChars());
  252. goto Lno;
  253. }
  254. // Find out which field index it is
  255. for (fieldi = 0; 1; fieldi++)
  256. {
  257. if (fieldi >= nfields)
  258. {
  259. s->error("is not a per-instance initializable field");
  260. goto Lno;
  261. }
  262. if (s == ad->fields.tdata()[fieldi])
  263. break;
  264. }
  265. }
  266. else if (fieldi >= nfields)
  267. { error(loc, "too many initializers for '%s'", ad->toChars());
  268. goto Lno;
  269. }
  270. Initializer *iz = value.tdata()[i];
  271. if (!iz)
  272. goto Lno;
  273. Expression *ex = iz->toExpression();
  274. if (!ex)
  275. goto Lno;
  276. if (elements->tdata()[fieldi])
  277. { error(loc, "duplicate initializer for field '%s'",
  278. ad->fields.tdata()[fieldi]->toChars());
  279. goto Lno;
  280. }
  281. elements->tdata()[fieldi] = ex;
  282. ++fieldi;
  283. }
  284. // Now, fill in any missing elements with default initializers.
  285. // We also need to validate any anonymous unions
  286. offset = 0;
  287. for (size_t i = 0; i < elements->dim; )
  288. {
  289. VarDeclaration * vd = ad->fields.tdata()[i]->isVarDeclaration();
  290. //printf("test2 [%d] : %s %d %d\n", i, vd->toChars(), (int)offset, (int)vd->offset);
  291. if (vd->offset < offset)
  292. {
  293. // Only the first field of a union can have an initializer
  294. if (elements->tdata()[i])
  295. goto Lno;
  296. }
  297. else
  298. {
  299. if (!elements->tdata()[i])
  300. // Default initialize
  301. elements->tdata()[i] = vd->type->defaultInit();
  302. }
  303. offset = vd->offset + vd->type->size();
  304. i++;
  305. #if 0
  306. int unionSize = ad->numFieldsInUnion(i);
  307. if (unionSize == 1)
  308. { // Not a union -- default initialize if missing
  309. if (!elements->tdata()[i])
  310. elements->tdata()[i] = vd->type->defaultInit();
  311. }
  312. else
  313. { // anonymous union -- check for errors
  314. int found = -1; // index of the first field with an initializer
  315. for (int j = i; j < i + unionSize; ++j)
  316. {
  317. if (!elements->tdata()[j])
  318. continue;
  319. if (found >= 0)
  320. {
  321. VarDeclaration * v1 = ((Dsymbol *)ad->fields.data[found])->isVarDeclaration();
  322. VarDeclaration * v = ((Dsymbol *)ad->fields.data[j])->isVarDeclaration();
  323. error(loc, "%s cannot have initializers for fields %s and %s in same union",
  324. ad->toChars(),
  325. v1->toChars(), v->toChars());
  326. goto Lno;
  327. }
  328. found = j;
  329. }
  330. if (found == -1)
  331. {
  332. error(loc, "no initializer for union that contains field %s",
  333. vd->toChars());
  334. goto Lno;
  335. }
  336. }
  337. i += unionSize;
  338. #endif
  339. }
  340. e = new StructLiteralExp(loc, sd, elements);
  341. e->type = sd->type;
  342. return e;
  343. Lno:
  344. delete elements;
  345. return NULL;
  346. }
  347. void StructInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  348. {
  349. //printf("StructInitializer::toCBuffer()\n");
  350. buf->writebyte('{');
  351. for (size_t i = 0; i < field.dim; i++)
  352. {
  353. if (i > 0)
  354. buf->writebyte(',');
  355. Identifier *id = field.tdata()[i];
  356. if (id)
  357. {
  358. buf->writestring(id->toChars());
  359. buf->writebyte(':');
  360. }
  361. Initializer *iz = value.tdata()[i];
  362. if (iz)
  363. iz->toCBuffer(buf, hgs);
  364. }
  365. buf->writebyte('}');
  366. }
  367. /********************************** ArrayInitializer ************************************/
  368. ArrayInitializer::ArrayInitializer(Loc loc)
  369. : Initializer(loc)
  370. {
  371. dim = 0;
  372. type = NULL;
  373. sem = 0;
  374. }
  375. Initializer *ArrayInitializer::syntaxCopy()
  376. {
  377. //printf("ArrayInitializer::syntaxCopy()\n");
  378. ArrayInitializer *ai = new ArrayInitializer(loc);
  379. assert(index.dim == value.dim);
  380. ai->index.setDim(index.dim);
  381. ai->value.setDim(value.dim);
  382. for (size_t i = 0; i < ai->value.dim; i++)
  383. { Expression *e = index.tdata()[i];
  384. if (e)
  385. e = e->syntaxCopy();
  386. ai->index.tdata()[i] = e;
  387. Initializer *init = value.tdata()[i];
  388. init = init->syntaxCopy();
  389. ai->value.tdata()[i] = init;
  390. }
  391. return ai;
  392. }
  393. void ArrayInitializer::addInit(Expression *index, Initializer *value)
  394. {
  395. this->index.push(index);
  396. this->value.push(value);
  397. dim = 0;
  398. type = NULL;
  399. }
  400. Initializer *ArrayInitializer::semantic(Scope *sc, Type *t, int needInterpret)
  401. { unsigned i;
  402. unsigned length;
  403. const uintmax_t amax = 0x80000000;
  404. //printf("ArrayInitializer::semantic(%s)\n", t->toChars());
  405. if (sem) // if semantic() already run
  406. return this;
  407. sem = 1;
  408. type = t;
  409. t = t->toBasetype();
  410. switch (t->ty)
  411. {
  412. case Tpointer:
  413. case Tsarray:
  414. case Tarray:
  415. break;
  416. default:
  417. error(loc, "cannot use array to initialize %s", type->toChars());
  418. goto Lerr;
  419. }
  420. length = 0;
  421. for (i = 0; i < index.dim; i++)
  422. {
  423. Expression *idx = index.tdata()[i];
  424. if (idx)
  425. { idx = idx->semantic(sc);
  426. idx = idx->optimize(WANTvalue | WANTinterpret);
  427. index.tdata()[i] = idx;
  428. length = idx->toInteger();
  429. }
  430. Initializer *val = value.tdata()[i];
  431. val = val->semantic(sc, t->nextOf(), needInterpret);
  432. value.tdata()[i] = val;
  433. length++;
  434. if (length == 0)
  435. { error(loc, "array dimension overflow");
  436. goto Lerr;
  437. }
  438. if (length > dim)
  439. dim = length;
  440. }
  441. if (t->ty == Tsarray)
  442. {
  443. dinteger_t edim = ((TypeSArray *)t)->dim->toInteger();
  444. if (dim > edim)
  445. {
  446. error(loc, "array initializer has %"PRIuTSIZE" elements, but array length is %"PRIdMAX, dim, edim);
  447. goto Lerr;
  448. }
  449. }
  450. if ((uintmax_t) dim * t->nextOf()->size() >= amax)
  451. { error(loc, "array dimension %"PRIuTSIZE" exceeds max of %"PRIuMAX, dim, amax / t->nextOf()->size());
  452. goto Lerr;
  453. }
  454. return this;
  455. Lerr:
  456. return new ExpInitializer(loc, new ErrorExp());
  457. }
  458. /********************************
  459. * If possible, convert array initializer to array literal.
  460. * Otherwise return NULL.
  461. */
  462. Expression *ArrayInitializer::toExpression()
  463. { Expressions *elements;
  464. //printf("ArrayInitializer::toExpression(), dim = %d\n", dim);
  465. //static int i; if (++i == 2) halt();
  466. size_t edim;
  467. Type *t = NULL;
  468. if (type)
  469. {
  470. if (type == Type::terror)
  471. return new ErrorExp();
  472. t = type->toBasetype();
  473. switch (t->ty)
  474. {
  475. case Tsarray:
  476. edim = ((TypeSArray *)t)->dim->toInteger();
  477. break;
  478. case Tpointer:
  479. case Tarray:
  480. edim = dim;
  481. break;
  482. default:
  483. assert(0);
  484. }
  485. }
  486. else
  487. {
  488. edim = value.dim;
  489. for (size_t i = 0, j = 0; i < value.dim; i++, j++)
  490. {
  491. if (index.tdata()[i])
  492. j = index.tdata()[i]->toInteger();
  493. if (j >= edim)
  494. edim = j + 1;
  495. }
  496. }
  497. elements = new Expressions();
  498. elements->setDim(edim);
  499. elements->zero();
  500. for (size_t i = 0, j = 0; i < value.dim; i++, j++)
  501. {
  502. if (index.tdata()[i])
  503. j = (index.tdata()[i])->toInteger();
  504. assert(j < edim);
  505. Initializer *iz = value.tdata()[i];
  506. if (!iz)
  507. goto Lno;
  508. Expression *ex = iz->toExpression();
  509. if (!ex)
  510. {
  511. goto Lno;
  512. }
  513. elements->tdata()[j] = ex;
  514. }
  515. /* Fill in any missing elements with the default initializer
  516. */
  517. {
  518. Expression *init = NULL;
  519. for (size_t i = 0; i < edim; i++)
  520. {
  521. if (!elements->tdata()[i])
  522. {
  523. if (!type)
  524. goto Lno;
  525. if (!init)
  526. init = ((TypeNext *)t)->next->defaultInit();
  527. elements->tdata()[i] = init;
  528. }
  529. }
  530. Expression *e = new ArrayLiteralExp(loc, elements);
  531. e->type = type;
  532. return e;
  533. }
  534. Lno:
  535. return NULL;
  536. }
  537. /********************************
  538. * If possible, convert array initializer to associative array initializer.
  539. */
  540. Expression *ArrayInitializer::toAssocArrayLiteral()
  541. {
  542. Expression *e;
  543. //printf("ArrayInitializer::toAssocArrayInitializer()\n");
  544. //static int i; if (++i == 2) halt();
  545. Expressions *keys = new Expressions();
  546. keys->setDim(value.dim);
  547. Expressions *values = new Expressions();
  548. values->setDim(value.dim);
  549. for (size_t i = 0; i < value.dim; i++)
  550. {
  551. e = index.tdata()[i];
  552. if (!e)
  553. goto Lno;
  554. keys->tdata()[i] = e;
  555. Initializer *iz = value.tdata()[i];
  556. if (!iz)
  557. goto Lno;
  558. e = iz->toExpression();
  559. if (!e)
  560. goto Lno;
  561. values->tdata()[i] = e;
  562. }
  563. e = new AssocArrayLiteralExp(loc, keys, values);
  564. return e;
  565. Lno:
  566. delete keys;
  567. delete values;
  568. error(loc, "not an associative array initializer");
  569. return new ErrorExp();
  570. }
  571. int ArrayInitializer::isAssociativeArray()
  572. {
  573. for (size_t i = 0; i < value.dim; i++)
  574. {
  575. if (index.tdata()[i])
  576. return 1;
  577. }
  578. return 0;
  579. }
  580. Type *ArrayInitializer::inferType(Scope *sc)
  581. {
  582. //printf("ArrayInitializer::inferType() %s\n", toChars());
  583. assert(0);
  584. return NULL;
  585. #if 0
  586. type = Type::terror;
  587. for (size_t i = 0; i < value.dim; i++)
  588. {
  589. if (index.data[i])
  590. goto Laa;
  591. }
  592. for (size_t i = 0; i < value.dim; i++)
  593. {
  594. Initializer *iz = (Initializer *)value.data[i];
  595. if (iz)
  596. { Type *t = iz->inferType(sc);
  597. if (i == 0)
  598. { /* BUG: This gets the type from the first element.
  599. * Fix to use all the elements to figure out the type.
  600. */
  601. t = new TypeSArray(t, new IntegerExp(value.dim));
  602. t = t->semantic(loc, sc);
  603. type = t;
  604. }
  605. }
  606. }
  607. return type;
  608. Laa:
  609. /* It's possibly an associative array initializer.
  610. * BUG: inferring type from first member.
  611. */
  612. Initializer *iz = (Initializer *)value.data[0];
  613. Expression *indexinit = (Expression *)index.data[0];
  614. if (iz && indexinit)
  615. { Type *t = iz->inferType(sc);
  616. indexinit = indexinit->semantic(sc);
  617. Type *indext = indexinit->type;
  618. t = new TypeAArray(t, indext);
  619. type = t->semantic(loc, sc);
  620. }
  621. else
  622. error(loc, "cannot infer type from this array initializer");
  623. return type;
  624. #endif
  625. }
  626. void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  627. {
  628. buf->writebyte('[');
  629. for (size_t i = 0; i < index.dim; i++)
  630. {
  631. if (i > 0)
  632. buf->writebyte(',');
  633. Expression *ex = index.tdata()[i];
  634. if (ex)
  635. {
  636. ex->toCBuffer(buf, hgs);
  637. buf->writebyte(':');
  638. }
  639. Initializer *iz = value.tdata()[i];
  640. if (iz)
  641. iz->toCBuffer(buf, hgs);
  642. }
  643. buf->writebyte(']');
  644. }
  645. /********************************** ExpInitializer ************************************/
  646. ExpInitializer::ExpInitializer(Loc loc, Expression *exp)
  647. : Initializer(loc)
  648. {
  649. this->exp = exp;
  650. }
  651. Initializer *ExpInitializer::syntaxCopy()
  652. {
  653. return new ExpInitializer(loc, exp->syntaxCopy());
  654. }
  655. bool arrayHasNonConstPointers(Expressions *elems);
  656. bool hasNonConstPointers(Expression *e)
  657. {
  658. if (e->op == TOKnull)
  659. return false;
  660. if (e->op == TOKstructliteral)
  661. {
  662. StructLiteralExp *se = (StructLiteralExp *)e;
  663. return arrayHasNonConstPointers(se->elements);
  664. }
  665. if (e->op == TOKarrayliteral)
  666. {
  667. if (!e->type->nextOf()->hasPointers())
  668. return false;
  669. ArrayLiteralExp *ae = (ArrayLiteralExp *)e;
  670. return arrayHasNonConstPointers(ae->elements);
  671. }
  672. if (e->op == TOKassocarrayliteral)
  673. {
  674. AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e;
  675. if (ae->type->nextOf()->hasPointers() &&
  676. arrayHasNonConstPointers(ae->values))
  677. return true;
  678. if (((TypeAArray *)ae->type)->index->hasPointers())
  679. return arrayHasNonConstPointers(ae->keys);
  680. return false;
  681. }
  682. if (e->type->ty== Tpointer && e->type->nextOf()->ty != Tfunction)
  683. {
  684. if (e->op == TOKsymoff) // address of a global is OK
  685. return false;
  686. if (e->op == TOKint64) // cast(void *)int is OK
  687. return false;
  688. if (e->op == TOKstring) // "abc".ptr is OK
  689. return false;
  690. return true;
  691. }
  692. return false;
  693. }
  694. bool arrayHasNonConstPointers(Expressions *elems)
  695. {
  696. for (size_t i = 0; i < elems->dim; i++)
  697. {
  698. if (!elems->tdata()[i])
  699. continue;
  700. if (hasNonConstPointers(elems->tdata()[i]))
  701. return true;
  702. }
  703. return false;
  704. }
  705. Initializer *ExpInitializer::semantic(Scope *sc, Type *t, int needInterpret)
  706. {
  707. //printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
  708. exp = exp->semantic(sc);
  709. exp = resolveProperties(sc, exp);
  710. int wantOptimize = needInterpret ? WANTinterpret|WANTvalue : WANTvalue;
  711. int olderrors = global.errors;
  712. exp = exp->optimize(wantOptimize);
  713. if (!global.gag && olderrors != global.errors)
  714. return this; // Failed, suppress duplicate error messages
  715. if (exp->op == TOKtype)
  716. error("initializer must be an expression, not '%s'", exp->toChars());
  717. // Make sure all pointers are constants
  718. if (needInterpret && hasNonConstPointers(exp))
  719. {
  720. exp->error("cannot use non-constant CTFE pointer in an initializer '%s'", exp->toChars());
  721. return this;
  722. }
  723. Type *tb = t->toBasetype();
  724. /* Look for case of initializing a static array with a too-short
  725. * string literal, such as:
  726. * char[5] foo = "abc";
  727. * Allow this by doing an explicit cast, which will lengthen the string
  728. * literal.
  729. */
  730. if (exp->op == TOKstring && tb->ty == Tsarray && exp->type->ty == Tsarray)
  731. { StringExp *se = (StringExp *)exp;
  732. if (!se->committed && se->type->ty == Tsarray &&
  733. ((TypeSArray *)se->type)->dim->toInteger() <
  734. ((TypeSArray *)t)->dim->toInteger())
  735. {
  736. exp = se->castTo(sc, t);
  737. goto L1;
  738. }
  739. }
  740. // Look for the case of statically initializing an array
  741. // with a single member.
  742. if (tb->ty == Tsarray &&
  743. !tb->nextOf()->equals(exp->type->toBasetype()->nextOf()) &&
  744. exp->implicitConvTo(tb->nextOf())
  745. )
  746. {
  747. t = tb->nextOf();
  748. }
  749. exp = exp->implicitCastTo(sc, t);
  750. L1:
  751. exp = exp->optimize(wantOptimize);
  752. //printf("-ExpInitializer::semantic(): "); exp->print();
  753. return this;
  754. }
  755. Type *ExpInitializer::inferType(Scope *sc)
  756. {
  757. //printf("ExpInitializer::inferType() %s\n", toChars());
  758. exp = exp->semantic(sc);
  759. exp = resolveProperties(sc, exp);
  760. // Give error for overloaded function addresses
  761. if (exp->op == TOKsymoff)
  762. { SymOffExp *se = (SymOffExp *)exp;
  763. if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique())
  764. exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
  765. }
  766. // Give error for overloaded function addresses
  767. if (exp->op == TOKdelegate)
  768. { DelegateExp *se = (DelegateExp *)exp;
  769. if (
  770. se->func->isFuncDeclaration() &&
  771. !se->func->isFuncDeclaration()->isUnique())
  772. exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
  773. }
  774. Type *t = exp->type;
  775. if (!t)
  776. t = Initializer::inferType(sc);
  777. return t;
  778. }
  779. Expression *ExpInitializer::toExpression()
  780. {
  781. return exp;
  782. }
  783. void ExpInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  784. {
  785. exp->toCBuffer(buf, hgs);
  786. }