1
|
1
|
|
2 // Copyright (c) 1999-2007 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
|
|
10 // Routines to perform function inlining
|
|
11
|
|
12 #define LOG 0
|
|
13
|
|
14 #include <stdio.h>
|
|
15 #include <stdlib.h>
|
|
16 #include <assert.h>
|
|
17
|
|
18 #include "id.h"
|
|
19 #include "init.h"
|
|
20 #include "declaration.h"
|
|
21 #include "aggregate.h"
|
|
22 #include "expression.h"
|
|
23 #include "statement.h"
|
|
24 #include "mtype.h"
|
|
25
|
|
26 /* ========== Compute cost of inlining =============== */
|
|
27
|
|
28 /* Walk trees to determine if inlining can be done, and if so,
|
|
29 * if it is too complex to be worth inlining or not.
|
|
30 */
|
|
31
|
|
32 struct InlineCostState
|
|
33 {
|
|
34 int nested;
|
|
35 int hasthis;
|
|
36 int hdrscan; // !=0 if inline scan for 'header' content
|
|
37 FuncDeclaration *fd;
|
|
38 };
|
|
39
|
|
40 const int COST_MAX = 250;
|
|
41
|
|
42 int Statement::inlineCost(InlineCostState *ics)
|
|
43 {
|
|
44 return COST_MAX; // default is we can't inline it
|
|
45 }
|
|
46
|
|
47 int ExpStatement::inlineCost(InlineCostState *ics)
|
|
48 {
|
|
49 return exp ? exp->inlineCost(ics) : 0;
|
|
50 }
|
|
51
|
|
52 int CompoundStatement::inlineCost(InlineCostState *ics)
|
|
53 { int cost = 0;
|
|
54
|
|
55 for (size_t i = 0; i < statements->dim; i++)
|
|
56 { Statement *s = (Statement *) statements->data[i];
|
|
57 if (s)
|
|
58 {
|
|
59 cost += s->inlineCost(ics);
|
|
60 if (cost >= COST_MAX)
|
|
61 break;
|
|
62 }
|
|
63 }
|
|
64 return cost;
|
|
65 }
|
|
66
|
|
67 int UnrolledLoopStatement::inlineCost(InlineCostState *ics)
|
|
68 { int cost = 0;
|
|
69
|
|
70 for (size_t i = 0; i < statements->dim; i++)
|
|
71 { Statement *s = (Statement *) statements->data[i];
|
|
72 if (s)
|
|
73 {
|
|
74 cost += s->inlineCost(ics);
|
|
75 if (cost >= COST_MAX)
|
|
76 break;
|
|
77 }
|
|
78 }
|
|
79 return cost;
|
|
80 }
|
|
81
|
|
82 int IfStatement::inlineCost(InlineCostState *ics)
|
|
83 {
|
|
84 int cost;
|
|
85
|
|
86 /* Can't declare variables inside ?: expressions, so
|
|
87 * we cannot inline if a variable is declared.
|
|
88 */
|
|
89 if (arg)
|
|
90 return COST_MAX;
|
|
91
|
|
92 cost = condition->inlineCost(ics);
|
|
93
|
|
94 /* Specifically allow:
|
|
95 * if (condition)
|
|
96 * return exp1;
|
|
97 * else
|
|
98 * return exp2;
|
|
99 * Otherwise, we can't handle return statements nested in if's.
|
|
100 */
|
|
101
|
|
102 if (elsebody && ifbody &&
|
|
103 ifbody->isReturnStatement() &&
|
|
104 elsebody->isReturnStatement())
|
|
105 {
|
|
106 cost += ifbody->inlineCost(ics);
|
|
107 cost += elsebody->inlineCost(ics);
|
|
108 //printf("cost = %d\n", cost);
|
|
109 }
|
|
110 else
|
|
111 {
|
|
112 ics->nested += 1;
|
|
113 if (ifbody)
|
|
114 cost += ifbody->inlineCost(ics);
|
|
115 if (elsebody)
|
|
116 cost += elsebody->inlineCost(ics);
|
|
117 ics->nested -= 1;
|
|
118 }
|
|
119 return cost;
|
|
120 }
|
|
121
|
|
122 int ReturnStatement::inlineCost(InlineCostState *ics)
|
|
123 {
|
|
124 // Can't handle return statements nested in if's
|
|
125 if (ics->nested)
|
|
126 return COST_MAX;
|
|
127 return exp ? exp->inlineCost(ics) : 0;
|
|
128 }
|
|
129
|
|
130 /* -------------------------- */
|
|
131
|
|
132 int arrayInlineCost(InlineCostState *ics, Array *arguments)
|
|
133 { int cost = 0;
|
|
134
|
|
135 if (arguments)
|
|
136 {
|
|
137 for (int i = 0; i < arguments->dim; i++)
|
|
138 { Expression *e = (Expression *)arguments->data[i];
|
|
139
|
|
140 if (e)
|
|
141 cost += e->inlineCost(ics);
|
|
142 }
|
|
143 }
|
|
144 return cost;
|
|
145 }
|
|
146
|
|
147 int Expression::inlineCost(InlineCostState *ics)
|
|
148 {
|
|
149 return 1;
|
|
150 }
|
|
151
|
|
152 int VarExp::inlineCost(InlineCostState *ics)
|
|
153 {
|
|
154 //printf("VarExp::inlineCost() %s\n", toChars());
|
|
155 return 1;
|
|
156 }
|
|
157
|
|
158 int ThisExp::inlineCost(InlineCostState *ics)
|
|
159 {
|
|
160 FuncDeclaration *fd = ics->fd;
|
|
161 if (!ics->hdrscan)
|
|
162 if (fd->isNested() || !ics->hasthis)
|
|
163 return COST_MAX;
|
|
164 return 1;
|
|
165 }
|
|
166
|
|
167 int SuperExp::inlineCost(InlineCostState *ics)
|
|
168 {
|
|
169 FuncDeclaration *fd = ics->fd;
|
|
170 if (!ics->hdrscan)
|
|
171 if (fd->isNested() || !ics->hasthis)
|
|
172 return COST_MAX;
|
|
173 return 1;
|
|
174 }
|
|
175
|
|
176 int TupleExp::inlineCost(InlineCostState *ics)
|
|
177 {
|
|
178 return 1 + arrayInlineCost(ics, exps);
|
|
179 }
|
|
180
|
|
181 int ArrayLiteralExp::inlineCost(InlineCostState *ics)
|
|
182 {
|
|
183 return 1 + arrayInlineCost(ics, elements);
|
|
184 }
|
|
185
|
|
186 int AssocArrayLiteralExp::inlineCost(InlineCostState *ics)
|
|
187 {
|
|
188 return 1 + arrayInlineCost(ics, keys) + arrayInlineCost(ics, values);
|
|
189 }
|
|
190
|
|
191 int StructLiteralExp::inlineCost(InlineCostState *ics)
|
|
192 {
|
|
193 return 1 + arrayInlineCost(ics, elements);
|
|
194 }
|
|
195
|
|
196 int FuncExp::inlineCost(InlineCostState *ics)
|
|
197 {
|
|
198 // Right now, this makes the function be output to the .obj file twice.
|
|
199 return COST_MAX;
|
|
200 }
|
|
201
|
|
202 int DelegateExp::inlineCost(InlineCostState *ics)
|
|
203 {
|
|
204 return COST_MAX;
|
|
205 }
|
|
206
|
|
207 int DeclarationExp::inlineCost(InlineCostState *ics)
|
|
208 { int cost = 0;
|
|
209 VarDeclaration *vd;
|
|
210
|
|
211 //printf("DeclarationExp::inlineCost()\n");
|
|
212 vd = declaration->isVarDeclaration();
|
|
213 if (vd)
|
|
214 {
|
|
215 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
|
|
216 if (td)
|
|
217 {
|
|
218 #if 1
|
|
219 return COST_MAX; // finish DeclarationExp::doInline
|
|
220 #else
|
|
221 for (size_t i = 0; i < td->objects->dim; i++)
|
|
222 { Object *o = (Object *)td->objects->data[i];
|
|
223 if (o->dyncast() != DYNCAST_EXPRESSION)
|
|
224 return COST_MAX;
|
|
225 Expression *eo = (Expression *)o;
|
|
226 if (eo->op != TOKdsymbol)
|
|
227 return COST_MAX;
|
|
228 }
|
|
229 return td->objects->dim;
|
|
230 #endif
|
|
231 }
|
|
232 if (!ics->hdrscan && vd->isDataseg())
|
|
233 return COST_MAX;
|
|
234 cost += 1;
|
|
235
|
|
236 // Scan initializer (vd->init)
|
|
237 if (vd->init)
|
|
238 {
|
|
239 ExpInitializer *ie = vd->init->isExpInitializer();
|
|
240
|
|
241 if (ie)
|
|
242 {
|
|
243 cost += ie->exp->inlineCost(ics);
|
|
244 }
|
|
245 }
|
|
246 }
|
|
247
|
|
248 // These can contain functions, which when copied, get output twice.
|
|
249 if (declaration->isStructDeclaration() ||
|
|
250 declaration->isClassDeclaration() ||
|
|
251 declaration->isFuncDeclaration() ||
|
|
252 declaration->isTypedefDeclaration() ||
|
|
253 declaration->isTemplateMixin())
|
|
254 return COST_MAX;
|
|
255
|
|
256 //printf("DeclarationExp::inlineCost('%s')\n", toChars());
|
|
257 return cost;
|
|
258 }
|
|
259
|
|
260 int UnaExp::inlineCost(InlineCostState *ics)
|
|
261 {
|
|
262 return 1 + e1->inlineCost(ics);
|
|
263 }
|
|
264
|
|
265 int AssertExp::inlineCost(InlineCostState *ics)
|
|
266 {
|
|
267 return 1 + e1->inlineCost(ics) + (msg ? msg->inlineCost(ics) : 0);
|
|
268 }
|
|
269
|
|
270 int BinExp::inlineCost(InlineCostState *ics)
|
|
271 {
|
|
272 return 1 + e1->inlineCost(ics) + e2->inlineCost(ics);
|
|
273 }
|
|
274
|
|
275 int CallExp::inlineCost(InlineCostState *ics)
|
|
276 {
|
|
277 return 1 + e1->inlineCost(ics) + arrayInlineCost(ics, arguments);
|
|
278 }
|
|
279
|
|
280 int SliceExp::inlineCost(InlineCostState *ics)
|
|
281 { int cost;
|
|
282
|
|
283 cost = 1 + e1->inlineCost(ics);
|
|
284 if (lwr)
|
|
285 cost += lwr->inlineCost(ics);
|
|
286 if (upr)
|
|
287 cost += upr->inlineCost(ics);
|
|
288 return cost;
|
|
289 }
|
|
290
|
|
291 int ArrayExp::inlineCost(InlineCostState *ics)
|
|
292 {
|
|
293 return 1 + e1->inlineCost(ics) + arrayInlineCost(ics, arguments);
|
|
294 }
|
|
295
|
|
296
|
|
297 int CondExp::inlineCost(InlineCostState *ics)
|
|
298 {
|
|
299 return 1 +
|
|
300 e1->inlineCost(ics) +
|
|
301 e2->inlineCost(ics) +
|
|
302 econd->inlineCost(ics);
|
|
303 }
|
|
304
|
|
305
|
|
306 /* ======================== Perform the inlining ============================== */
|
|
307
|
|
308 /* Inlining is done by:
|
|
309 * o Converting to an Expression
|
|
310 * o Copying the trees of the function to be inlined
|
|
311 * o Renaming the variables
|
|
312 */
|
|
313
|
|
314 struct InlineDoState
|
|
315 {
|
|
316 VarDeclaration *vthis;
|
|
317 Array from; // old Dsymbols
|
|
318 Array to; // parallel array of new Dsymbols
|
|
319 Dsymbol *parent; // new parent
|
|
320 };
|
|
321
|
|
322 Expression *Statement::doInline(InlineDoState *ids)
|
|
323 {
|
|
324 assert(0);
|
|
325 return NULL; // default is we can't inline it
|
|
326 }
|
|
327
|
|
328 Expression *ExpStatement::doInline(InlineDoState *ids)
|
|
329 {
|
|
330 #if LOG
|
|
331 if (exp) printf("ExpStatement::doInline() '%s'\n", exp->toChars());
|
|
332 #endif
|
|
333 return exp ? exp->doInline(ids) : NULL;
|
|
334 }
|
|
335
|
|
336 Expression *CompoundStatement::doInline(InlineDoState *ids)
|
|
337 {
|
|
338 Expression *e = NULL;
|
|
339
|
|
340 //printf("CompoundStatement::doInline() %d\n", statements->dim);
|
|
341 for (size_t i = 0; i < statements->dim; i++)
|
|
342 { Statement *s = (Statement *) statements->data[i];
|
|
343 if (s)
|
|
344 {
|
|
345 Expression *e2 = s->doInline(ids);
|
|
346 e = Expression::combine(e, e2);
|
|
347 if (s->isReturnStatement())
|
|
348 break;
|
|
349
|
|
350 /* Check for:
|
|
351 * if (condition)
|
|
352 * return exp1;
|
|
353 * else
|
|
354 * return exp2;
|
|
355 */
|
|
356 IfStatement *ifs = s->isIfStatement();
|
|
357 if (ifs && ifs->elsebody && ifs->ifbody &&
|
|
358 ifs->ifbody->isReturnStatement() &&
|
|
359 ifs->elsebody->isReturnStatement()
|
|
360 )
|
|
361 break;
|
|
362
|
|
363 }
|
|
364 }
|
|
365 return e;
|
|
366 }
|
|
367
|
|
368 Expression *UnrolledLoopStatement::doInline(InlineDoState *ids)
|
|
369 {
|
|
370 Expression *e = NULL;
|
|
371
|
|
372 //printf("UnrolledLoopStatement::doInline() %d\n", statements->dim);
|
|
373 for (size_t i = 0; i < statements->dim; i++)
|
|
374 { Statement *s = (Statement *) statements->data[i];
|
|
375 if (s)
|
|
376 {
|
|
377 Expression *e2 = s->doInline(ids);
|
|
378 e = Expression::combine(e, e2);
|
|
379 if (s->isReturnStatement())
|
|
380 break;
|
|
381 }
|
|
382 }
|
|
383 return e;
|
|
384 }
|
|
385
|
|
386 Expression *IfStatement::doInline(InlineDoState *ids)
|
|
387 {
|
|
388 Expression *econd;
|
|
389 Expression *e1;
|
|
390 Expression *e2;
|
|
391 Expression *e;
|
|
392
|
|
393 assert(!arg);
|
|
394 econd = condition->doInline(ids);
|
|
395 assert(econd);
|
|
396 if (ifbody)
|
|
397 e1 = ifbody->doInline(ids);
|
|
398 else
|
|
399 e1 = NULL;
|
|
400 if (elsebody)
|
|
401 e2 = elsebody->doInline(ids);
|
|
402 else
|
|
403 e2 = NULL;
|
|
404 if (e1 && e2)
|
|
405 {
|
|
406 e = new CondExp(econd->loc, econd, e1, e2);
|
|
407 e->type = e1->type;
|
|
408 }
|
|
409 else if (e1)
|
|
410 {
|
|
411 e = new AndAndExp(econd->loc, econd, e1);
|
|
412 e->type = Type::tvoid;
|
|
413 }
|
|
414 else if (e2)
|
|
415 {
|
|
416 e = new OrOrExp(econd->loc, econd, e2);
|
|
417 e->type = Type::tvoid;
|
|
418 }
|
|
419 else
|
|
420 {
|
|
421 e = econd;
|
|
422 }
|
|
423 return e;
|
|
424 }
|
|
425
|
|
426 Expression *ReturnStatement::doInline(InlineDoState *ids)
|
|
427 {
|
|
428 //printf("ReturnStatement::doInline() '%s'\n", exp ? exp->toChars() : "");
|
|
429 return exp ? exp->doInline(ids) : 0;
|
|
430 }
|
|
431
|
|
432 /* --------------------------------------------------------------- */
|
|
433
|
|
434 /******************************
|
|
435 * Perform doInline() on an array of Expressions.
|
|
436 */
|
|
437
|
|
438 Expressions *arrayExpressiondoInline(Expressions *a, InlineDoState *ids)
|
|
439 { Expressions *newa = NULL;
|
|
440
|
|
441 if (a)
|
|
442 {
|
|
443 newa = new Expressions();
|
|
444 newa->setDim(a->dim);
|
|
445
|
|
446 for (int i = 0; i < a->dim; i++)
|
|
447 { Expression *e = (Expression *)a->data[i];
|
|
448
|
|
449 if (e)
|
|
450 {
|
|
451 e = e->doInline(ids);
|
|
452 newa->data[i] = (void *)e;
|
|
453 }
|
|
454 }
|
|
455 }
|
|
456 return newa;
|
|
457 }
|
|
458
|
|
459 Expression *Expression::doInline(InlineDoState *ids)
|
|
460 {
|
|
461 //printf("Expression::doInline(%s): %s\n", Token::toChars(op), toChars());
|
|
462 return copy();
|
|
463 }
|
|
464
|
|
465 Expression *SymOffExp::doInline(InlineDoState *ids)
|
|
466 {
|
|
467 int i;
|
|
468
|
|
469 //printf("SymOffExp::doInline(%s)\n", toChars());
|
|
470 for (i = 0; i < ids->from.dim; i++)
|
|
471 {
|
|
472 if (var == (Declaration *)ids->from.data[i])
|
|
473 {
|
|
474 SymOffExp *se = (SymOffExp *)copy();
|
|
475
|
|
476 se->var = (Declaration *)ids->to.data[i];
|
|
477 return se;
|
|
478 }
|
|
479 }
|
|
480 return this;
|
|
481 }
|
|
482
|
|
483 Expression *VarExp::doInline(InlineDoState *ids)
|
|
484 {
|
|
485 int i;
|
|
486
|
|
487 //printf("VarExp::doInline(%s)\n", toChars());
|
|
488 for (i = 0; i < ids->from.dim; i++)
|
|
489 {
|
|
490 if (var == (Declaration *)ids->from.data[i])
|
|
491 {
|
|
492 VarExp *ve = (VarExp *)copy();
|
|
493
|
|
494 ve->var = (Declaration *)ids->to.data[i];
|
|
495 return ve;
|
|
496 }
|
|
497 }
|
|
498 return this;
|
|
499 }
|
|
500
|
|
501 Expression *ThisExp::doInline(InlineDoState *ids)
|
|
502 {
|
|
503 //if (!ids->vthis)
|
|
504 //error("no 'this' when inlining %s", ids->parent->toChars());
|
|
505 if (!ids->vthis)
|
|
506 {
|
|
507 return this;
|
|
508 }
|
|
509
|
|
510 VarExp *ve = new VarExp(loc, ids->vthis);
|
|
511 ve->type = type;
|
|
512 return ve;
|
|
513 }
|
|
514
|
|
515 Expression *SuperExp::doInline(InlineDoState *ids)
|
|
516 {
|
|
517 assert(ids->vthis);
|
|
518
|
|
519 VarExp *ve = new VarExp(loc, ids->vthis);
|
|
520 ve->type = type;
|
|
521 return ve;
|
|
522 }
|
|
523
|
|
524 Expression *DeclarationExp::doInline(InlineDoState *ids)
|
|
525 { DeclarationExp *de = (DeclarationExp *)copy();
|
|
526 VarDeclaration *vd;
|
|
527
|
|
528 //printf("DeclarationExp::doInline(%s)\n", toChars());
|
|
529 vd = declaration->isVarDeclaration();
|
|
530 if (vd)
|
|
531 {
|
|
532 #if 0
|
|
533 // Need to figure this out before inlining can work for tuples
|
|
534 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
|
|
535 if (td)
|
|
536 {
|
|
537 for (size_t i = 0; i < td->objects->dim; i++)
|
|
538 { DsymbolExp *se = (DsymbolExp *)td->objects->data[i];
|
|
539 assert(se->op == TOKdsymbol);
|
|
540 se->s;
|
|
541 }
|
|
542 return st->objects->dim;
|
|
543 }
|
|
544 #endif
|
|
545 if (vd->isStatic() || vd->isConst())
|
|
546 ;
|
|
547 else
|
|
548 {
|
|
549 VarDeclaration *vto;
|
|
550
|
|
551 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
|
|
552 *vto = *vd;
|
|
553 vto->parent = ids->parent;
|
|
554 vto->csym = NULL;
|
|
555 vto->isym = NULL;
|
|
556
|
|
557 ids->from.push(vd);
|
|
558 ids->to.push(vto);
|
|
559
|
92
|
560 if (vd->init)
|
1
|
561 {
|
92
|
562 if (vd->init->isVoidInitializer())
|
|
563 {
|
|
564 vto->init = new VoidInitializer(vd->init->loc);
|
|
565 }
|
|
566 else
|
|
567 {
|
|
568 ExpInitializer *ie = vd->init->isExpInitializer();
|
|
569 assert(ie);
|
|
570 vto->init = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
|
|
571 }
|
1
|
572 }
|
|
573 de->declaration = (Dsymbol *) (void *)vto;
|
|
574 }
|
|
575 }
|
|
576 /* This needs work, like DeclarationExp::toElem(), if we are
|
|
577 * to handle TemplateMixin's. For now, we just don't inline them.
|
|
578 */
|
|
579 return de;
|
|
580 }
|
|
581
|
|
582 Expression *NewExp::doInline(InlineDoState *ids)
|
|
583 {
|
|
584 //printf("NewExp::doInline(): %s\n", toChars());
|
|
585 NewExp *ne = (NewExp *)copy();
|
|
586
|
|
587 if (thisexp)
|
|
588 ne->thisexp = thisexp->doInline(ids);
|
|
589 ne->newargs = arrayExpressiondoInline(ne->newargs, ids);
|
|
590 ne->arguments = arrayExpressiondoInline(ne->arguments, ids);
|
|
591 return ne;
|
|
592 }
|
|
593
|
|
594 Expression *UnaExp::doInline(InlineDoState *ids)
|
|
595 {
|
|
596 UnaExp *ue = (UnaExp *)copy();
|
|
597
|
|
598 ue->e1 = e1->doInline(ids);
|
|
599 return ue;
|
|
600 }
|
|
601
|
|
602 Expression *AssertExp::doInline(InlineDoState *ids)
|
|
603 {
|
|
604 AssertExp *ae = (AssertExp *)copy();
|
|
605
|
|
606 ae->e1 = e1->doInline(ids);
|
|
607 if (msg)
|
|
608 ae->msg = msg->doInline(ids);
|
|
609 return ae;
|
|
610 }
|
|
611
|
|
612 Expression *BinExp::doInline(InlineDoState *ids)
|
|
613 {
|
|
614 BinExp *be = (BinExp *)copy();
|
|
615
|
|
616 be->e1 = e1->doInline(ids);
|
|
617 be->e2 = e2->doInline(ids);
|
|
618 return be;
|
|
619 }
|
|
620
|
|
621 Expression *CallExp::doInline(InlineDoState *ids)
|
|
622 {
|
|
623 CallExp *ce;
|
|
624
|
|
625 ce = (CallExp *)copy();
|
|
626 ce->e1 = e1->doInline(ids);
|
|
627 ce->arguments = arrayExpressiondoInline(arguments, ids);
|
|
628 return ce;
|
|
629 }
|
|
630
|
|
631
|
|
632 Expression *IndexExp::doInline(InlineDoState *ids)
|
|
633 {
|
|
634 IndexExp *are = (IndexExp *)copy();
|
|
635
|
|
636 are->e1 = e1->doInline(ids);
|
|
637
|
|
638 if (lengthVar)
|
|
639 { //printf("lengthVar\n");
|
|
640 VarDeclaration *vd = lengthVar;
|
|
641 ExpInitializer *ie;
|
|
642 ExpInitializer *ieto;
|
|
643 VarDeclaration *vto;
|
|
644
|
|
645 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
|
|
646 *vto = *vd;
|
|
647 vto->parent = ids->parent;
|
|
648 vto->csym = NULL;
|
|
649 vto->isym = NULL;
|
|
650
|
|
651 ids->from.push(vd);
|
|
652 ids->to.push(vto);
|
|
653
|
|
654 if (vd->init)
|
|
655 {
|
|
656 ie = vd->init->isExpInitializer();
|
|
657 assert(ie);
|
|
658 ieto = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
|
|
659 vto->init = ieto;
|
|
660 }
|
|
661
|
|
662 are->lengthVar = (VarDeclaration *) (void *)vto;
|
|
663 }
|
|
664 are->e2 = e2->doInline(ids);
|
|
665 return are;
|
|
666 }
|
|
667
|
|
668
|
|
669 Expression *SliceExp::doInline(InlineDoState *ids)
|
|
670 {
|
|
671 SliceExp *are = (SliceExp *)copy();
|
|
672
|
|
673 are->e1 = e1->doInline(ids);
|
|
674
|
|
675 if (lengthVar)
|
|
676 { //printf("lengthVar\n");
|
|
677 VarDeclaration *vd = lengthVar;
|
|
678 ExpInitializer *ie;
|
|
679 ExpInitializer *ieto;
|
|
680 VarDeclaration *vto;
|
|
681
|
|
682 vto = new VarDeclaration(vd->loc, vd->type, vd->ident, vd->init);
|
|
683 *vto = *vd;
|
|
684 vto->parent = ids->parent;
|
|
685 vto->csym = NULL;
|
|
686 vto->isym = NULL;
|
|
687
|
|
688 ids->from.push(vd);
|
|
689 ids->to.push(vto);
|
|
690
|
|
691 if (vd->init)
|
|
692 {
|
|
693 ie = vd->init->isExpInitializer();
|
|
694 assert(ie);
|
|
695 ieto = new ExpInitializer(ie->loc, ie->exp->doInline(ids));
|
|
696 vto->init = ieto;
|
|
697 }
|
|
698
|
|
699 are->lengthVar = (VarDeclaration *) (void *)vto;
|
|
700 }
|
|
701 if (lwr)
|
|
702 are->lwr = lwr->doInline(ids);
|
|
703 if (upr)
|
|
704 are->upr = upr->doInline(ids);
|
|
705 return are;
|
|
706 }
|
|
707
|
|
708
|
|
709 Expression *TupleExp::doInline(InlineDoState *ids)
|
|
710 {
|
|
711 TupleExp *ce;
|
|
712
|
|
713 ce = (TupleExp *)copy();
|
|
714 ce->exps = arrayExpressiondoInline(exps, ids);
|
|
715 return ce;
|
|
716 }
|
|
717
|
|
718
|
|
719 Expression *ArrayLiteralExp::doInline(InlineDoState *ids)
|
|
720 {
|
|
721 ArrayLiteralExp *ce;
|
|
722
|
|
723 ce = (ArrayLiteralExp *)copy();
|
|
724 ce->elements = arrayExpressiondoInline(elements, ids);
|
|
725 return ce;
|
|
726 }
|
|
727
|
|
728
|
|
729 Expression *AssocArrayLiteralExp::doInline(InlineDoState *ids)
|
|
730 {
|
|
731 AssocArrayLiteralExp *ce;
|
|
732
|
|
733 ce = (AssocArrayLiteralExp *)copy();
|
|
734 ce->keys = arrayExpressiondoInline(keys, ids);
|
|
735 ce->values = arrayExpressiondoInline(values, ids);
|
|
736 return ce;
|
|
737 }
|
|
738
|
|
739
|
|
740 Expression *StructLiteralExp::doInline(InlineDoState *ids)
|
|
741 {
|
|
742 StructLiteralExp *ce;
|
|
743
|
|
744 ce = (StructLiteralExp *)copy();
|
|
745 ce->elements = arrayExpressiondoInline(elements, ids);
|
|
746 return ce;
|
|
747 }
|
|
748
|
|
749
|
|
750 Expression *ArrayExp::doInline(InlineDoState *ids)
|
|
751 {
|
|
752 ArrayExp *ce;
|
|
753
|
|
754 ce = (ArrayExp *)copy();
|
|
755 ce->e1 = e1->doInline(ids);
|
|
756 ce->arguments = arrayExpressiondoInline(arguments, ids);
|
|
757 return ce;
|
|
758 }
|
|
759
|
|
760
|
|
761 Expression *CondExp::doInline(InlineDoState *ids)
|
|
762 {
|
|
763 CondExp *ce = (CondExp *)copy();
|
|
764
|
|
765 ce->econd = econd->doInline(ids);
|
|
766 ce->e1 = e1->doInline(ids);
|
|
767 ce->e2 = e2->doInline(ids);
|
|
768 return ce;
|
|
769 }
|
|
770
|
|
771
|
|
772 /* ========== Walk the parse trees, and inline expand functions ============= */
|
|
773
|
|
774 /* Walk the trees, looking for functions to inline.
|
|
775 * Inline any that can be.
|
|
776 */
|
|
777
|
|
778 struct InlineScanState
|
|
779 {
|
|
780 FuncDeclaration *fd; // function being scanned
|
|
781 };
|
|
782
|
|
783 Statement *Statement::inlineScan(InlineScanState *iss)
|
|
784 {
|
|
785 return this;
|
|
786 }
|
|
787
|
|
788 Statement *ExpStatement::inlineScan(InlineScanState *iss)
|
|
789 {
|
|
790 #if LOG
|
|
791 printf("ExpStatement::inlineScan(%s)\n", toChars());
|
|
792 #endif
|
|
793 if (exp)
|
|
794 exp = exp->inlineScan(iss);
|
|
795 return this;
|
|
796 }
|
|
797
|
|
798 Statement *CompoundStatement::inlineScan(InlineScanState *iss)
|
|
799 {
|
|
800 for (size_t i = 0; i < statements->dim; i++)
|
|
801 { Statement *s = (Statement *) statements->data[i];
|
|
802 if (s)
|
|
803 statements->data[i] = (void *)s->inlineScan(iss);
|
|
804 }
|
|
805 return this;
|
|
806 }
|
|
807
|
|
808 Statement *UnrolledLoopStatement::inlineScan(InlineScanState *iss)
|
|
809 {
|
|
810 for (size_t i = 0; i < statements->dim; i++)
|
|
811 { Statement *s = (Statement *) statements->data[i];
|
|
812 if (s)
|
|
813 statements->data[i] = (void *)s->inlineScan(iss);
|
|
814 }
|
|
815 return this;
|
|
816 }
|
|
817
|
|
818 Statement *ScopeStatement::inlineScan(InlineScanState *iss)
|
|
819 {
|
|
820 if (statement)
|
|
821 statement = statement->inlineScan(iss);
|
|
822 return this;
|
|
823 }
|
|
824
|
|
825 Statement *WhileStatement::inlineScan(InlineScanState *iss)
|
|
826 {
|
|
827 condition = condition->inlineScan(iss);
|
|
828 body = body ? body->inlineScan(iss) : NULL;
|
|
829 return this;
|
|
830 }
|
|
831
|
|
832
|
|
833 Statement *DoStatement::inlineScan(InlineScanState *iss)
|
|
834 {
|
|
835 body = body ? body->inlineScan(iss) : NULL;
|
|
836 condition = condition->inlineScan(iss);
|
|
837 return this;
|
|
838 }
|
|
839
|
|
840
|
|
841 Statement *ForStatement::inlineScan(InlineScanState *iss)
|
|
842 {
|
|
843 if (init)
|
|
844 init = init->inlineScan(iss);
|
|
845 if (condition)
|
|
846 condition = condition->inlineScan(iss);
|
|
847 if (increment)
|
|
848 increment = increment->inlineScan(iss);
|
|
849 body = body->inlineScan(iss);
|
|
850 return this;
|
|
851 }
|
|
852
|
|
853
|
|
854 Statement *ForeachStatement::inlineScan(InlineScanState *iss)
|
|
855 {
|
|
856 aggr = aggr->inlineScan(iss);
|
92
|
857 if (body)
|
|
858 body = body->inlineScan(iss);
|
1
|
859 return this;
|
|
860 }
|
|
861
|
|
862
|
336
|
863 #if DMDV2
|
1
|
864 Statement *ForeachRangeStatement::inlineScan(InlineScanState *iss)
|
|
865 {
|
|
866 lwr = lwr->inlineScan(iss);
|
|
867 upr = upr->inlineScan(iss);
|
92
|
868 if (body)
|
|
869 body = body->inlineScan(iss);
|
1
|
870 return this;
|
|
871 }
|
|
872 #endif
|
|
873
|
|
874
|
|
875 Statement *IfStatement::inlineScan(InlineScanState *iss)
|
|
876 {
|
|
877 condition = condition->inlineScan(iss);
|
|
878 if (ifbody)
|
|
879 ifbody = ifbody->inlineScan(iss);
|
|
880 if (elsebody)
|
|
881 elsebody = elsebody->inlineScan(iss);
|
|
882 return this;
|
|
883 }
|
|
884
|
|
885
|
|
886 Statement *SwitchStatement::inlineScan(InlineScanState *iss)
|
|
887 {
|
|
888 //printf("SwitchStatement::inlineScan()\n");
|
|
889 condition = condition->inlineScan(iss);
|
|
890 body = body ? body->inlineScan(iss) : NULL;
|
|
891 if (sdefault)
|
|
892 sdefault = (DefaultStatement *)sdefault->inlineScan(iss);
|
|
893 if (cases)
|
|
894 {
|
|
895 for (int i = 0; i < cases->dim; i++)
|
|
896 { Statement *s;
|
|
897
|
|
898 s = (Statement *) cases->data[i];
|
|
899 cases->data[i] = (void *)s->inlineScan(iss);
|
|
900 }
|
|
901 }
|
|
902 return this;
|
|
903 }
|
|
904
|
|
905
|
|
906 Statement *CaseStatement::inlineScan(InlineScanState *iss)
|
|
907 {
|
|
908 //printf("CaseStatement::inlineScan()\n");
|
|
909 exp = exp->inlineScan(iss);
|
|
910 if (statement)
|
|
911 statement = statement->inlineScan(iss);
|
|
912 return this;
|
|
913 }
|
|
914
|
|
915
|
|
916 Statement *DefaultStatement::inlineScan(InlineScanState *iss)
|
|
917 {
|
|
918 if (statement)
|
|
919 statement = statement->inlineScan(iss);
|
|
920 return this;
|
|
921 }
|
|
922
|
|
923
|
|
924 Statement *ReturnStatement::inlineScan(InlineScanState *iss)
|
|
925 {
|
|
926 if (exp)
|
|
927 exp = exp->inlineScan(iss);
|
|
928 return this;
|
|
929 }
|
|
930
|
|
931
|
|
932 Statement *SynchronizedStatement::inlineScan(InlineScanState *iss)
|
|
933 {
|
|
934 if (exp)
|
|
935 exp = exp->inlineScan(iss);
|
|
936 if (body)
|
|
937 body = body->inlineScan(iss);
|
|
938 return this;
|
|
939 }
|
|
940
|
|
941
|
|
942 Statement *WithStatement::inlineScan(InlineScanState *iss)
|
|
943 {
|
|
944 if (exp)
|
|
945 exp = exp->inlineScan(iss);
|
|
946 if (body)
|
|
947 body = body->inlineScan(iss);
|
|
948 return this;
|
|
949 }
|
|
950
|
|
951
|
|
952 Statement *TryCatchStatement::inlineScan(InlineScanState *iss)
|
|
953 {
|
|
954 if (body)
|
|
955 body = body->inlineScan(iss);
|
|
956 if (catches)
|
|
957 {
|
|
958 for (int i = 0; i < catches->dim; i++)
|
|
959 { Catch *c = (Catch *)catches->data[i];
|
|
960
|
|
961 if (c->handler)
|
|
962 c->handler = c->handler->inlineScan(iss);
|
|
963 }
|
|
964 }
|
|
965 return this;
|
|
966 }
|
|
967
|
|
968
|
|
969 Statement *TryFinallyStatement::inlineScan(InlineScanState *iss)
|
|
970 {
|
|
971 if (body)
|
|
972 body = body->inlineScan(iss);
|
|
973 if (finalbody)
|
|
974 finalbody = finalbody->inlineScan(iss);
|
|
975 return this;
|
|
976 }
|
|
977
|
|
978
|
|
979 Statement *ThrowStatement::inlineScan(InlineScanState *iss)
|
|
980 {
|
|
981 if (exp)
|
|
982 exp = exp->inlineScan(iss);
|
|
983 return this;
|
|
984 }
|
|
985
|
|
986
|
|
987 Statement *VolatileStatement::inlineScan(InlineScanState *iss)
|
|
988 {
|
|
989 if (statement)
|
|
990 statement = statement->inlineScan(iss);
|
|
991 return this;
|
|
992 }
|
|
993
|
|
994
|
|
995 Statement *LabelStatement::inlineScan(InlineScanState *iss)
|
|
996 {
|
|
997 if (statement)
|
|
998 statement = statement->inlineScan(iss);
|
|
999 return this;
|
|
1000 }
|
|
1001
|
|
1002 /* -------------------------- */
|
|
1003
|
|
1004 void arrayInlineScan(InlineScanState *iss, Array *arguments)
|
|
1005 {
|
|
1006 if (arguments)
|
|
1007 {
|
|
1008 for (int i = 0; i < arguments->dim; i++)
|
|
1009 { Expression *e = (Expression *)arguments->data[i];
|
|
1010
|
|
1011 if (e)
|
|
1012 {
|
|
1013 e = e->inlineScan(iss);
|
|
1014 arguments->data[i] = (void *)e;
|
|
1015 }
|
|
1016 }
|
|
1017 }
|
|
1018 }
|
|
1019
|
|
1020 Expression *Expression::inlineScan(InlineScanState *iss)
|
|
1021 {
|
|
1022 return this;
|
|
1023 }
|
|
1024
|
|
1025 void scanVar(Dsymbol *s, InlineScanState *iss)
|
|
1026 {
|
|
1027 VarDeclaration *vd = s->isVarDeclaration();
|
|
1028 if (vd)
|
|
1029 {
|
|
1030 TupleDeclaration *td = vd->toAlias()->isTupleDeclaration();
|
|
1031 if (td)
|
|
1032 {
|
|
1033 for (size_t i = 0; i < td->objects->dim; i++)
|
|
1034 { DsymbolExp *se = (DsymbolExp *)td->objects->data[i];
|
|
1035 assert(se->op == TOKdsymbol);
|
|
1036 scanVar(se->s, iss);
|
|
1037 }
|
|
1038 }
|
|
1039 else
|
|
1040 {
|
|
1041 // Scan initializer (vd->init)
|
|
1042 if (vd->init)
|
|
1043 {
|
|
1044 ExpInitializer *ie = vd->init->isExpInitializer();
|
|
1045
|
|
1046 if (ie)
|
|
1047 {
|
|
1048 ie->exp = ie->exp->inlineScan(iss);
|
|
1049 }
|
|
1050 }
|
|
1051 }
|
|
1052 }
|
|
1053 }
|
|
1054
|
|
1055 Expression *DeclarationExp::inlineScan(InlineScanState *iss)
|
|
1056 {
|
|
1057 //printf("DeclarationExp::inlineScan()\n");
|
|
1058 scanVar(declaration, iss);
|
|
1059 return this;
|
|
1060 }
|
|
1061
|
|
1062 Expression *UnaExp::inlineScan(InlineScanState *iss)
|
|
1063 {
|
|
1064 e1 = e1->inlineScan(iss);
|
|
1065 return this;
|
|
1066 }
|
|
1067
|
|
1068 Expression *AssertExp::inlineScan(InlineScanState *iss)
|
|
1069 {
|
|
1070 e1 = e1->inlineScan(iss);
|
|
1071 if (msg)
|
|
1072 msg = msg->inlineScan(iss);
|
|
1073 return this;
|
|
1074 }
|
|
1075
|
|
1076 Expression *BinExp::inlineScan(InlineScanState *iss)
|
|
1077 {
|
|
1078 e1 = e1->inlineScan(iss);
|
|
1079 e2 = e2->inlineScan(iss);
|
|
1080 return this;
|
|
1081 }
|
|
1082
|
|
1083
|
|
1084 Expression *CallExp::inlineScan(InlineScanState *iss)
|
|
1085 { Expression *e = this;
|
|
1086
|
|
1087 //printf("CallExp::inlineScan()\n");
|
|
1088 e1 = e1->inlineScan(iss);
|
|
1089 arrayInlineScan(iss, arguments);
|
|
1090
|
|
1091 if (e1->op == TOKvar)
|
|
1092 {
|
|
1093 VarExp *ve = (VarExp *)e1;
|
|
1094 FuncDeclaration *fd = ve->var->isFuncDeclaration();
|
|
1095
|
|
1096 if (fd && fd != iss->fd && fd->canInline(0))
|
|
1097 {
|
|
1098 e = fd->doInline(iss, NULL, arguments);
|
|
1099 }
|
|
1100 }
|
|
1101 else if (e1->op == TOKdotvar)
|
|
1102 {
|
|
1103 DotVarExp *dve = (DotVarExp *)e1;
|
|
1104 FuncDeclaration *fd = dve->var->isFuncDeclaration();
|
|
1105
|
|
1106 if (fd && fd != iss->fd && fd->canInline(1))
|
|
1107 {
|
|
1108 if (dve->e1->op == TOKcall &&
|
|
1109 dve->e1->type->toBasetype()->ty == Tstruct)
|
|
1110 {
|
|
1111 /* To create ethis, we'll need to take the address
|
|
1112 * of dve->e1, but this won't work if dve->e1 is
|
|
1113 * a function call.
|
|
1114 */
|
|
1115 ;
|
|
1116 }
|
|
1117 else
|
|
1118 e = fd->doInline(iss, dve->e1, arguments);
|
|
1119 }
|
|
1120 }
|
|
1121
|
|
1122 return e;
|
|
1123 }
|
|
1124
|
|
1125
|
|
1126 Expression *SliceExp::inlineScan(InlineScanState *iss)
|
|
1127 {
|
|
1128 e1 = e1->inlineScan(iss);
|
|
1129 if (lwr)
|
|
1130 lwr = lwr->inlineScan(iss);
|
|
1131 if (upr)
|
|
1132 upr = upr->inlineScan(iss);
|
|
1133 return this;
|
|
1134 }
|
|
1135
|
|
1136
|
|
1137 Expression *TupleExp::inlineScan(InlineScanState *iss)
|
|
1138 { Expression *e = this;
|
|
1139
|
|
1140 //printf("TupleExp::inlineScan()\n");
|
|
1141 arrayInlineScan(iss, exps);
|
|
1142
|
|
1143 return e;
|
|
1144 }
|
|
1145
|
|
1146
|
|
1147 Expression *ArrayLiteralExp::inlineScan(InlineScanState *iss)
|
|
1148 { Expression *e = this;
|
|
1149
|
|
1150 //printf("ArrayLiteralExp::inlineScan()\n");
|
|
1151 arrayInlineScan(iss, elements);
|
|
1152
|
|
1153 return e;
|
|
1154 }
|
|
1155
|
|
1156
|
|
1157 Expression *AssocArrayLiteralExp::inlineScan(InlineScanState *iss)
|
|
1158 { Expression *e = this;
|
|
1159
|
|
1160 //printf("AssocArrayLiteralExp::inlineScan()\n");
|
|
1161 arrayInlineScan(iss, keys);
|
|
1162 arrayInlineScan(iss, values);
|
|
1163
|
|
1164 return e;
|
|
1165 }
|
|
1166
|
|
1167
|
|
1168 Expression *StructLiteralExp::inlineScan(InlineScanState *iss)
|
|
1169 { Expression *e = this;
|
|
1170
|
|
1171 //printf("StructLiteralExp::inlineScan()\n");
|
|
1172 arrayInlineScan(iss, elements);
|
|
1173
|
|
1174 return e;
|
|
1175 }
|
|
1176
|
|
1177
|
|
1178 Expression *ArrayExp::inlineScan(InlineScanState *iss)
|
|
1179 { Expression *e = this;
|
|
1180
|
|
1181 //printf("ArrayExp::inlineScan()\n");
|
|
1182 e1 = e1->inlineScan(iss);
|
|
1183 arrayInlineScan(iss, arguments);
|
|
1184
|
|
1185 return e;
|
|
1186 }
|
|
1187
|
|
1188
|
|
1189 Expression *CondExp::inlineScan(InlineScanState *iss)
|
|
1190 {
|
|
1191 econd = econd->inlineScan(iss);
|
|
1192 e1 = e1->inlineScan(iss);
|
|
1193 e2 = e2->inlineScan(iss);
|
|
1194 return this;
|
|
1195 }
|
|
1196
|
|
1197
|
|
1198 /* ========== =============== */
|
|
1199
|
|
1200 void FuncDeclaration::inlineScan()
|
|
1201 {
|
|
1202 InlineScanState iss;
|
|
1203
|
|
1204 #if LOG
|
|
1205 printf("FuncDeclaration::inlineScan('%s')\n", toChars());
|
|
1206 #endif
|
|
1207 memset(&iss, 0, sizeof(iss));
|
|
1208 iss.fd = this;
|
|
1209 if (fbody)
|
|
1210 {
|
|
1211 inlineNest++;
|
|
1212 fbody = fbody->inlineScan(&iss);
|
|
1213 inlineNest--;
|
|
1214 }
|
|
1215 }
|
|
1216
|
|
1217 int FuncDeclaration::canInline(int hasthis, int hdrscan)
|
|
1218 {
|
|
1219 InlineCostState ics;
|
|
1220 int cost;
|
|
1221
|
|
1222 #define CANINLINE_LOG 0
|
|
1223
|
|
1224 #if CANINLINE_LOG
|
|
1225 printf("FuncDeclaration::canInline(hasthis = %d, '%s')\n", hasthis, toChars());
|
|
1226 #endif
|
|
1227
|
|
1228 if (needThis() && !hasthis)
|
|
1229 return 0;
|
|
1230
|
|
1231 if (inlineNest || (!semanticRun && !hdrscan))
|
|
1232 {
|
|
1233 #if CANINLINE_LOG
|
|
1234 printf("\t1: no, inlineNest = %d, semanticRun = %d\n", inlineNest, semanticRun);
|
|
1235 #endif
|
|
1236 return 0;
|
|
1237 }
|
|
1238
|
|
1239 switch (inlineStatus)
|
|
1240 {
|
|
1241 case ILSyes:
|
|
1242 #if CANINLINE_LOG
|
|
1243 printf("\tyes\n");
|
|
1244 #endif
|
|
1245 return 1;
|
|
1246
|
|
1247 case ILSno:
|
|
1248 #if CANINLINE_LOG
|
|
1249 printf("\t2: no\n");
|
|
1250 #endif
|
|
1251 return 0;
|
|
1252
|
|
1253 case ILSuninitialized:
|
|
1254 break;
|
|
1255
|
|
1256 default:
|
|
1257 assert(0);
|
|
1258 }
|
|
1259
|
|
1260 if (type)
|
|
1261 { assert(type->ty == Tfunction);
|
|
1262 TypeFunction *tf = (TypeFunction *)(type);
|
|
1263 if (tf->varargs == 1) // no variadic parameter lists
|
|
1264 goto Lno;
|
|
1265
|
|
1266 /* Don't inline a function that returns non-void, but has
|
|
1267 * no return expression.
|
|
1268 */
|
|
1269 if (tf->next && tf->next->ty != Tvoid &&
|
|
1270 !(hasReturnExp & 1) &&
|
|
1271 !hdrscan)
|
|
1272 goto Lno;
|
|
1273 }
|
|
1274 else
|
|
1275 { CtorDeclaration *ctor = isCtorDeclaration();
|
|
1276
|
|
1277 if (ctor && ctor->varargs == 1)
|
|
1278 goto Lno;
|
|
1279 }
|
|
1280
|
|
1281 if (
|
|
1282 !fbody ||
|
|
1283 !hdrscan &&
|
|
1284 (
|
|
1285 #if 0
|
|
1286 isCtorDeclaration() || // cannot because need to convert:
|
|
1287 // return;
|
|
1288 // to:
|
|
1289 // return this;
|
|
1290 #endif
|
|
1291 isSynchronized() ||
|
|
1292 isImportedSymbol() ||
|
336
|
1293 #if DMDV2
|
92
|
1294 closureVars.dim || // no nested references to this frame
|
|
1295 #else
|
1
|
1296 nestedFrameRef || // no nested references to this frame
|
92
|
1297 #endif
|
1
|
1298 (isVirtual() && !isFinal())
|
|
1299 ))
|
|
1300 {
|
|
1301 goto Lno;
|
|
1302 }
|
|
1303
|
|
1304 /* If any parameters are Tsarray's (which are passed by reference)
|
|
1305 * or out parameters (also passed by reference), don't do inlining.
|
|
1306 */
|
|
1307 if (parameters)
|
|
1308 {
|
|
1309 for (int i = 0; i < parameters->dim; i++)
|
|
1310 {
|
|
1311 VarDeclaration *v = (VarDeclaration *)parameters->data[i];
|
|
1312 if (v->isOut() || v->isRef() || v->type->toBasetype()->ty == Tsarray)
|
|
1313 goto Lno;
|
|
1314 }
|
|
1315 }
|
|
1316
|
|
1317 memset(&ics, 0, sizeof(ics));
|
|
1318 ics.hasthis = hasthis;
|
|
1319 ics.fd = this;
|
|
1320 ics.hdrscan = hdrscan;
|
|
1321 cost = fbody->inlineCost(&ics);
|
|
1322 #if CANINLINE_LOG
|
|
1323 printf("cost = %d\n", cost);
|
|
1324 #endif
|
|
1325 if (cost >= COST_MAX)
|
|
1326 goto Lno;
|
|
1327
|
|
1328 if (!hdrscan) // Don't scan recursively for header content scan
|
|
1329 inlineScan();
|
|
1330
|
|
1331 Lyes:
|
|
1332 if (!hdrscan) // Don't modify inlineStatus for header content scan
|
|
1333 inlineStatus = ILSyes;
|
|
1334 #if CANINLINE_LOG
|
|
1335 printf("\tyes\n");
|
|
1336 #endif
|
|
1337 return 1;
|
|
1338
|
|
1339 Lno:
|
|
1340 if (!hdrscan) // Don't modify inlineStatus for header content scan
|
|
1341 inlineStatus = ILSno;
|
|
1342 #if CANINLINE_LOG
|
|
1343 printf("\tno\n");
|
|
1344 #endif
|
|
1345 return 0;
|
|
1346 }
|
|
1347
|
|
1348 Expression *FuncDeclaration::doInline(InlineScanState *iss, Expression *ethis, Array *arguments)
|
|
1349 {
|
|
1350 InlineDoState ids;
|
|
1351 DeclarationExp *de;
|
|
1352 Expression *e = NULL;
|
|
1353
|
|
1354 #if LOG
|
|
1355 printf("FuncDeclaration::doInline('%s')\n", toChars());
|
|
1356 #endif
|
|
1357
|
|
1358 memset(&ids, 0, sizeof(ids));
|
|
1359 ids.parent = iss->fd;
|
|
1360
|
|
1361 // Set up vthis
|
|
1362 if (ethis)
|
|
1363 {
|
|
1364 VarDeclaration *vthis;
|
|
1365 ExpInitializer *ei;
|
|
1366 VarExp *ve;
|
|
1367
|
|
1368 if (ethis->type->ty != Tclass && ethis->type->ty != Tpointer)
|
|
1369 {
|
|
1370 ethis = ethis->addressOf(NULL);
|
|
1371 }
|
|
1372
|
|
1373 ei = new ExpInitializer(ethis->loc, ethis);
|
|
1374
|
|
1375 vthis = new VarDeclaration(ethis->loc, ethis->type, Id::This, ei);
|
|
1376 vthis->storage_class = STCin;
|
|
1377 vthis->linkage = LINKd;
|
|
1378 vthis->parent = iss->fd;
|
|
1379
|
|
1380 ve = new VarExp(vthis->loc, vthis);
|
|
1381 ve->type = vthis->type;
|
|
1382
|
|
1383 ei->exp = new AssignExp(vthis->loc, ve, ethis);
|
|
1384 ei->exp->type = ve->type;
|
|
1385
|
|
1386 ids.vthis = vthis;
|
|
1387 }
|
|
1388
|
|
1389 // Set up parameters
|
|
1390 if (ethis)
|
|
1391 {
|
|
1392 e = new DeclarationExp(0, ids.vthis);
|
|
1393 e->type = Type::tvoid;
|
|
1394 }
|
|
1395
|
|
1396 if (arguments && arguments->dim)
|
|
1397 {
|
|
1398 assert(parameters->dim == arguments->dim);
|
|
1399
|
|
1400 for (int i = 0; i < arguments->dim; i++)
|
|
1401 {
|
|
1402 VarDeclaration *vfrom = (VarDeclaration *)parameters->data[i];
|
|
1403 VarDeclaration *vto;
|
|
1404 Expression *arg = (Expression *)arguments->data[i];
|
|
1405 ExpInitializer *ei;
|
|
1406 VarExp *ve;
|
|
1407
|
|
1408 ei = new ExpInitializer(arg->loc, arg);
|
|
1409
|
|
1410 vto = new VarDeclaration(vfrom->loc, vfrom->type, vfrom->ident, ei);
|
|
1411 vto->storage_class |= vfrom->storage_class & (STCin | STCout | STClazy | STCref);
|
|
1412 vto->linkage = vfrom->linkage;
|
|
1413 vto->parent = iss->fd;
|
|
1414 //printf("vto = '%s', vto->storage_class = x%x\n", vto->toChars(), vto->storage_class);
|
|
1415 //printf("vto->parent = '%s'\n", iss->fd->toChars());
|
|
1416
|
|
1417 ve = new VarExp(vto->loc, vto);
|
|
1418 //ve->type = vto->type;
|
|
1419 ve->type = arg->type;
|
|
1420
|
|
1421 ei->exp = new AssignExp(vto->loc, ve, arg);
|
|
1422 ei->exp->type = ve->type;
|
|
1423 //ve->type->print();
|
|
1424 //arg->type->print();
|
|
1425 //ei->exp->print();
|
|
1426
|
|
1427 ids.from.push(vfrom);
|
|
1428 ids.to.push(vto);
|
|
1429
|
|
1430 de = new DeclarationExp(0, vto);
|
|
1431 de->type = Type::tvoid;
|
|
1432
|
|
1433 e = Expression::combine(e, de);
|
|
1434 }
|
|
1435 }
|
|
1436
|
|
1437 inlineNest++;
|
|
1438 Expression *eb = fbody->doInline(&ids);
|
|
1439 inlineNest--;
|
|
1440 //eb->type->print();
|
|
1441 //eb->print();
|
|
1442 //eb->dump(0);
|
|
1443 return Expression::combine(e, eb);
|
|
1444 }
|
|
1445
|
|
1446
|
|
1447
|