PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/dmd2/init.c

https://bitbucket.org/lindquist/ldc/
C | 716 lines | 558 code | 97 blank | 61 comment | 101 complexity | f1c11ffa3381e11b7bb996fad457aec9 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0
  1. // Compiler implementation of the D programming language
  2. // Copyright (c) 1999-2009 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. #include <stdio.h>
  10. #include <assert.h>
  11. #include "mars.h"
  12. #include "init.h"
  13. #include "expression.h"
  14. #include "statement.h"
  15. #include "identifier.h"
  16. #include "declaration.h"
  17. #include "aggregate.h"
  18. #include "scope.h"
  19. #include "mtype.h"
  20. #include "hdrgen.h"
  21. /********************************** Initializer *******************************/
  22. Initializer::Initializer(Loc loc)
  23. {
  24. this->loc = loc;
  25. }
  26. Initializer *Initializer::syntaxCopy()
  27. {
  28. return this;
  29. }
  30. Initializer *Initializer::semantic(Scope *sc, Type *t)
  31. {
  32. return this;
  33. }
  34. Type *Initializer::inferType(Scope *sc)
  35. {
  36. error(loc, "cannot infer type from initializer");
  37. return Type::terror;
  38. }
  39. Initializers *Initializer::arraySyntaxCopy(Initializers *ai)
  40. { Initializers *a = NULL;
  41. if (ai)
  42. {
  43. a = new Initializers();
  44. a->setDim(ai->dim);
  45. for (int i = 0; i < a->dim; i++)
  46. { Initializer *e = (Initializer *)ai->data[i];
  47. e = e->syntaxCopy();
  48. a->data[i] = e;
  49. }
  50. }
  51. return a;
  52. }
  53. char *Initializer::toChars()
  54. { OutBuffer *buf;
  55. HdrGenState hgs;
  56. memset(&hgs, 0, sizeof(hgs));
  57. buf = new OutBuffer();
  58. toCBuffer(buf, &hgs);
  59. return buf->toChars();
  60. }
  61. /********************************** VoidInitializer ***************************/
  62. VoidInitializer::VoidInitializer(Loc loc)
  63. : Initializer(loc)
  64. {
  65. type = NULL;
  66. }
  67. Initializer *VoidInitializer::syntaxCopy()
  68. {
  69. return new VoidInitializer(loc);
  70. }
  71. Initializer *VoidInitializer::semantic(Scope *sc, Type *t)
  72. {
  73. //printf("VoidInitializer::semantic(t = %p)\n", t);
  74. type = t;
  75. return this;
  76. }
  77. Expression *VoidInitializer::toExpression()
  78. {
  79. error(loc, "void initializer has no value");
  80. return new IntegerExp(0);
  81. }
  82. void VoidInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  83. {
  84. buf->writestring("void");
  85. }
  86. /********************************** StructInitializer *************************/
  87. StructInitializer::StructInitializer(Loc loc)
  88. : Initializer(loc)
  89. {
  90. ad = NULL;
  91. }
  92. Initializer *StructInitializer::syntaxCopy()
  93. {
  94. StructInitializer *ai = new StructInitializer(loc);
  95. assert(field.dim == value.dim);
  96. ai->field.setDim(field.dim);
  97. ai->value.setDim(value.dim);
  98. for (int i = 0; i < field.dim; i++)
  99. {
  100. ai->field.data[i] = field.data[i];
  101. Initializer *init = (Initializer *)value.data[i];
  102. init = init->syntaxCopy();
  103. ai->value.data[i] = init;
  104. }
  105. return ai;
  106. }
  107. void StructInitializer::addInit(Identifier *field, Initializer *value)
  108. {
  109. //printf("StructInitializer::addInit(field = %p, value = %p)\n", field, value);
  110. this->field.push(field);
  111. this->value.push(value);
  112. }
  113. Initializer *StructInitializer::semantic(Scope *sc, Type *t)
  114. {
  115. int errors = 0;
  116. //printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
  117. vars.setDim(field.dim);
  118. t = t->toBasetype();
  119. if (t->ty == Tstruct)
  120. {
  121. unsigned fieldi = 0;
  122. TypeStruct *ts = (TypeStruct *)t;
  123. ad = ts->sym;
  124. if (ad->ctor)
  125. error(loc, "%s %s has constructors, cannot use { initializers }, use %s( initializers ) instead",
  126. ad->kind(), ad->toChars(), ad->toChars());
  127. for (size_t i = 0; i < field.dim; i++)
  128. {
  129. Identifier *id = (Identifier *)field.data[i];
  130. Initializer *val = (Initializer *)value.data[i];
  131. Dsymbol *s;
  132. VarDeclaration *v;
  133. if (id == NULL)
  134. {
  135. if (fieldi >= ad->fields.dim)
  136. { error(loc, "too many initializers for %s", ad->toChars());
  137. field.remove(i);
  138. i--;
  139. continue;
  140. }
  141. else
  142. {
  143. s = (Dsymbol *)ad->fields.data[fieldi];
  144. }
  145. }
  146. else
  147. {
  148. //s = ad->symtab->lookup(id);
  149. s = ad->search(loc, id, 0);
  150. if (!s)
  151. {
  152. error(loc, "'%s' is not a member of '%s'", id->toChars(), t->toChars());
  153. continue;
  154. }
  155. // Find out which field index it is
  156. for (fieldi = 0; 1; fieldi++)
  157. {
  158. if (fieldi >= ad->fields.dim)
  159. {
  160. s->error("is not a per-instance initializable field");
  161. break;
  162. }
  163. if (s == (Dsymbol *)ad->fields.data[fieldi])
  164. break;
  165. }
  166. }
  167. if (s && (v = s->isVarDeclaration()) != NULL)
  168. {
  169. val = val->semantic(sc, v->type);
  170. value.data[i] = (void *)val;
  171. vars.data[i] = (void *)v;
  172. }
  173. else
  174. { error(loc, "%s is not a field of %s", id ? id->toChars() : s->toChars(), ad->toChars());
  175. errors = 1;
  176. }
  177. fieldi++;
  178. }
  179. }
  180. else if (t->ty == Tdelegate && value.dim == 0)
  181. { /* Rewrite as empty delegate literal { }
  182. */
  183. Parameters *arguments = new Parameters;
  184. Type *tf = new TypeFunction(arguments, NULL, 0, LINKd);
  185. FuncLiteralDeclaration *fd = new FuncLiteralDeclaration(loc, 0, tf, TOKdelegate, NULL);
  186. fd->fbody = new CompoundStatement(loc, new Statements());
  187. fd->endloc = loc;
  188. Expression *e = new FuncExp(loc, fd);
  189. ExpInitializer *ie = new ExpInitializer(loc, e);
  190. return ie->semantic(sc, t);
  191. }
  192. else
  193. {
  194. error(loc, "a struct is not a valid initializer for a %s", t->toChars());
  195. errors = 1;
  196. }
  197. if (errors)
  198. {
  199. field.setDim(0);
  200. value.setDim(0);
  201. vars.setDim(0);
  202. }
  203. return this;
  204. }
  205. /***************************************
  206. * This works by transforming a struct initializer into
  207. * a struct literal. In the future, the two should be the
  208. * same thing.
  209. */
  210. Expression *StructInitializer::toExpression()
  211. { Expression *e;
  212. //printf("StructInitializer::toExpression() %s\n", toChars());
  213. if (!ad) // if fwd referenced
  214. {
  215. return NULL;
  216. }
  217. StructDeclaration *sd = ad->isStructDeclaration();
  218. if (!sd)
  219. return NULL;
  220. Expressions *elements = new Expressions();
  221. for (size_t i = 0; i < value.dim; i++)
  222. {
  223. if (field.data[i])
  224. goto Lno;
  225. Initializer *iz = (Initializer *)value.data[i];
  226. if (!iz)
  227. goto Lno;
  228. Expression *ex = iz->toExpression();
  229. if (!ex)
  230. goto Lno;
  231. elements->push(ex);
  232. }
  233. e = new StructLiteralExp(loc, sd, elements);
  234. e->type = sd->type;
  235. return e;
  236. Lno:
  237. delete elements;
  238. //error(loc, "struct initializers as expressions are not allowed");
  239. return NULL;
  240. }
  241. void StructInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  242. {
  243. //printf("StructInitializer::toCBuffer()\n");
  244. buf->writebyte('{');
  245. for (int i = 0; i < field.dim; i++)
  246. {
  247. if (i > 0)
  248. buf->writebyte(',');
  249. Identifier *id = (Identifier *)field.data[i];
  250. if (id)
  251. {
  252. buf->writestring(id->toChars());
  253. buf->writebyte(':');
  254. }
  255. Initializer *iz = (Initializer *)value.data[i];
  256. if (iz)
  257. iz->toCBuffer(buf, hgs);
  258. }
  259. buf->writebyte('}');
  260. }
  261. /********************************** ArrayInitializer ************************************/
  262. ArrayInitializer::ArrayInitializer(Loc loc)
  263. : Initializer(loc)
  264. {
  265. dim = 0;
  266. type = NULL;
  267. sem = 0;
  268. }
  269. Initializer *ArrayInitializer::syntaxCopy()
  270. {
  271. //printf("ArrayInitializer::syntaxCopy()\n");
  272. ArrayInitializer *ai = new ArrayInitializer(loc);
  273. assert(index.dim == value.dim);
  274. ai->index.setDim(index.dim);
  275. ai->value.setDim(value.dim);
  276. for (int i = 0; i < ai->value.dim; i++)
  277. { Expression *e = (Expression *)index.data[i];
  278. if (e)
  279. e = e->syntaxCopy();
  280. ai->index.data[i] = e;
  281. Initializer *init = (Initializer *)value.data[i];
  282. init = init->syntaxCopy();
  283. ai->value.data[i] = init;
  284. }
  285. return ai;
  286. }
  287. void ArrayInitializer::addInit(Expression *index, Initializer *value)
  288. {
  289. this->index.push(index);
  290. this->value.push(value);
  291. dim = 0;
  292. type = NULL;
  293. }
  294. Initializer *ArrayInitializer::semantic(Scope *sc, Type *t)
  295. { unsigned i;
  296. unsigned length;
  297. const unsigned amax = 0x80000000;
  298. //printf("ArrayInitializer::semantic(%s)\n", t->toChars());
  299. if (sem) // if semantic() already run
  300. return this;
  301. sem = 1;
  302. type = t;
  303. t = t->toBasetype();
  304. switch (t->ty)
  305. {
  306. case Tpointer:
  307. case Tsarray:
  308. case Tarray:
  309. break;
  310. default:
  311. error(loc, "cannot use array to initialize %s", type->toChars());
  312. goto Lerr;
  313. }
  314. length = 0;
  315. for (i = 0; i < index.dim; i++)
  316. {
  317. Expression *idx = (Expression *)index.data[i];
  318. if (idx)
  319. { idx = idx->semantic(sc);
  320. idx = idx->optimize(WANTvalue | WANTinterpret);
  321. index.data[i] = (void *)idx;
  322. length = idx->toInteger();
  323. }
  324. Initializer *val = (Initializer *)value.data[i];
  325. val = val->semantic(sc, t->nextOf());
  326. value.data[i] = (void *)val;
  327. length++;
  328. if (length == 0)
  329. { error(loc, "array dimension overflow");
  330. goto Lerr;
  331. }
  332. if (length > dim)
  333. dim = length;
  334. }
  335. if (t->ty == Tsarray)
  336. {
  337. dinteger_t edim = ((TypeSArray *)t)->dim->toInteger();
  338. if (dim > edim)
  339. {
  340. error(loc, "array initializer has %u elements, but array length is %jd", dim, edim);
  341. goto Lerr;
  342. }
  343. }
  344. if ((unsigned long) dim * t->nextOf()->size() >= amax)
  345. { error(loc, "array dimension %u exceeds max of %u", dim, amax / t->nextOf()->size());
  346. goto Lerr;
  347. }
  348. return this;
  349. Lerr:
  350. return new ExpInitializer(loc, new ErrorExp());
  351. }
  352. /********************************
  353. * If possible, convert array initializer to array literal.
  354. * Otherwise return NULL.
  355. */
  356. Expression *ArrayInitializer::toExpression()
  357. { Expressions *elements;
  358. Expression *e;
  359. //printf("ArrayInitializer::toExpression(), dim = %d\n", dim);
  360. //static int i; if (++i == 2) halt();
  361. size_t edim;
  362. Type *t = NULL;
  363. if (type)
  364. {
  365. if (type == Type::terror)
  366. return new ErrorExp();
  367. t = type->toBasetype();
  368. switch (t->ty)
  369. {
  370. case Tsarray:
  371. edim = ((TypeSArray *)t)->dim->toInteger();
  372. break;
  373. case Tpointer:
  374. case Tarray:
  375. edim = dim;
  376. break;
  377. default:
  378. assert(0);
  379. }
  380. }
  381. else
  382. {
  383. edim = value.dim;
  384. for (size_t i = 0, j = 0; i < value.dim; i++, j++)
  385. {
  386. if (index.data[i])
  387. j = ((Expression *)index.data[i])->toInteger();
  388. if (j >= edim)
  389. edim = j + 1;
  390. }
  391. }
  392. elements = new Expressions();
  393. elements->setDim(edim);
  394. elements->zero();
  395. for (size_t i = 0, j = 0; i < value.dim; i++, j++)
  396. {
  397. if (index.data[i])
  398. j = ((Expression *)index.data[i])->toInteger();
  399. assert(j < edim);
  400. Initializer *iz = (Initializer *)value.data[i];
  401. if (!iz)
  402. goto Lno;
  403. Expression *ex = iz->toExpression();
  404. if (!ex)
  405. {
  406. goto Lno;
  407. }
  408. elements->data[j] = ex;
  409. }
  410. /* Fill in any missing elements with the default initializer
  411. */
  412. {
  413. Expression *init = NULL;
  414. for (size_t i = 0; i < edim; i++)
  415. {
  416. if (!elements->data[i])
  417. {
  418. if (!type)
  419. goto Lno;
  420. if (!init)
  421. init = ((TypeNext *)t)->next->defaultInit();
  422. elements->data[i] = init;
  423. }
  424. }
  425. Expression *e = new ArrayLiteralExp(loc, elements);
  426. e->type = type;
  427. return e;
  428. }
  429. Lno:
  430. return NULL;
  431. }
  432. /********************************
  433. * If possible, convert array initializer to associative array initializer.
  434. */
  435. Expression *ArrayInitializer::toAssocArrayLiteral()
  436. {
  437. Expression *e;
  438. //printf("ArrayInitializer::toAssocArrayInitializer()\n");
  439. //static int i; if (++i == 2) halt();
  440. Expressions *keys = new Expressions();
  441. keys->setDim(value.dim);
  442. Expressions *values = new Expressions();
  443. values->setDim(value.dim);
  444. for (size_t i = 0; i < value.dim; i++)
  445. {
  446. e = (Expression *)index.data[i];
  447. if (!e)
  448. goto Lno;
  449. keys->data[i] = (void *)e;
  450. Initializer *iz = (Initializer *)value.data[i];
  451. if (!iz)
  452. goto Lno;
  453. e = iz->toExpression();
  454. if (!e)
  455. goto Lno;
  456. values->data[i] = (void *)e;
  457. }
  458. e = new AssocArrayLiteralExp(loc, keys, values);
  459. return e;
  460. Lno:
  461. delete keys;
  462. delete values;
  463. error(loc, "not an associative array initializer");
  464. return new ErrorExp();
  465. }
  466. int ArrayInitializer::isAssociativeArray()
  467. {
  468. for (size_t i = 0; i < value.dim; i++)
  469. {
  470. if (index.data[i])
  471. return 1;
  472. }
  473. return 0;
  474. }
  475. Type *ArrayInitializer::inferType(Scope *sc)
  476. {
  477. //printf("ArrayInitializer::inferType() %s\n", toChars());
  478. assert(0);
  479. return NULL;
  480. #if 0
  481. type = Type::terror;
  482. for (size_t i = 0; i < value.dim; i++)
  483. {
  484. if (index.data[i])
  485. goto Laa;
  486. }
  487. for (size_t i = 0; i < value.dim; i++)
  488. {
  489. Initializer *iz = (Initializer *)value.data[i];
  490. if (iz)
  491. { Type *t = iz->inferType(sc);
  492. if (i == 0)
  493. { /* BUG: This gets the type from the first element.
  494. * Fix to use all the elements to figure out the type.
  495. */
  496. t = new TypeSArray(t, new IntegerExp(value.dim));
  497. t = t->semantic(loc, sc);
  498. type = t;
  499. }
  500. }
  501. }
  502. return type;
  503. Laa:
  504. /* It's possibly an associative array initializer.
  505. * BUG: inferring type from first member.
  506. */
  507. Initializer *iz = (Initializer *)value.data[0];
  508. Expression *indexinit = (Expression *)index.data[0];
  509. if (iz && indexinit)
  510. { Type *t = iz->inferType(sc);
  511. indexinit = indexinit->semantic(sc);
  512. Type *indext = indexinit->type;
  513. t = new TypeAArray(t, indext);
  514. type = t->semantic(loc, sc);
  515. }
  516. else
  517. error(loc, "cannot infer type from this array initializer");
  518. return type;
  519. #endif
  520. }
  521. void ArrayInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  522. {
  523. buf->writebyte('[');
  524. for (int i = 0; i < index.dim; i++)
  525. {
  526. if (i > 0)
  527. buf->writebyte(',');
  528. Expression *ex = (Expression *)index.data[i];
  529. if (ex)
  530. {
  531. ex->toCBuffer(buf, hgs);
  532. buf->writebyte(':');
  533. }
  534. Initializer *iz = (Initializer *)value.data[i];
  535. if (iz)
  536. iz->toCBuffer(buf, hgs);
  537. }
  538. buf->writebyte(']');
  539. }
  540. /********************************** ExpInitializer ************************************/
  541. ExpInitializer::ExpInitializer(Loc loc, Expression *exp)
  542. : Initializer(loc)
  543. {
  544. this->exp = exp;
  545. }
  546. Initializer *ExpInitializer::syntaxCopy()
  547. {
  548. return new ExpInitializer(loc, exp->syntaxCopy());
  549. }
  550. Initializer *ExpInitializer::semantic(Scope *sc, Type *t)
  551. {
  552. //printf("ExpInitializer::semantic(%s), type = %s\n", exp->toChars(), t->toChars());
  553. exp = exp->semantic(sc);
  554. exp = resolveProperties(sc, exp);
  555. exp = exp->optimize(WANTvalue | WANTinterpret);
  556. Type *tb = t->toBasetype();
  557. /* Look for case of initializing a static array with a too-short
  558. * string literal, such as:
  559. * char[5] foo = "abc";
  560. * Allow this by doing an explicit cast, which will lengthen the string
  561. * literal.
  562. */
  563. if (exp->op == TOKstring && tb->ty == Tsarray && exp->type->ty == Tsarray)
  564. { StringExp *se = (StringExp *)exp;
  565. if (!se->committed && se->type->ty == Tsarray &&
  566. ((TypeSArray *)se->type)->dim->toInteger() <
  567. ((TypeSArray *)t)->dim->toInteger())
  568. {
  569. exp = se->castTo(sc, t);
  570. goto L1;
  571. }
  572. }
  573. // Look for the case of statically initializing an array
  574. // with a single member.
  575. if (tb->ty == Tsarray &&
  576. !tb->nextOf()->equals(exp->type->toBasetype()->nextOf()) &&
  577. exp->implicitConvTo(tb->nextOf())
  578. )
  579. {
  580. t = tb->nextOf();
  581. }
  582. exp = exp->implicitCastTo(sc, t);
  583. L1:
  584. exp = exp->optimize(WANTvalue | WANTinterpret);
  585. //printf("-ExpInitializer::semantic(): "); exp->print();
  586. return this;
  587. }
  588. Type *ExpInitializer::inferType(Scope *sc)
  589. {
  590. //printf("ExpInitializer::inferType() %s\n", toChars());
  591. exp = exp->semantic(sc);
  592. exp = resolveProperties(sc, exp);
  593. // Give error for overloaded function addresses
  594. if (exp->op == TOKsymoff)
  595. { SymOffExp *se = (SymOffExp *)exp;
  596. if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique())
  597. exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
  598. }
  599. // Give error for overloaded function addresses
  600. if (exp->op == TOKdelegate)
  601. { DelegateExp *se = (DelegateExp *)exp;
  602. if (
  603. se->func->isFuncDeclaration() &&
  604. !se->func->isFuncDeclaration()->isUnique())
  605. exp->error("cannot infer type from overloaded function symbol %s", exp->toChars());
  606. }
  607. Type *t = exp->type;
  608. if (!t)
  609. t = Initializer::inferType(sc);
  610. return t;
  611. }
  612. Expression *ExpInitializer::toExpression()
  613. {
  614. return exp;
  615. }
  616. void ExpInitializer::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
  617. {
  618. exp->toCBuffer(buf, hgs);
  619. }