2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
10 #include "root/dsystem.h"
11 #include "root/rmem.h"
12 #include "root/checkedint.h"
15 #include "statement.h"
17 #include "expression.h"
20 #include "staticassert.h"
23 #include "declaration.h"
24 #include "aggregate.h"
32 StorageClass
mergeFuncAttrs(StorageClass s1
, FuncDeclaration
*f
);
33 bool checkReturnEscapeRef(Scope
*sc
, Expression
*e
, bool gag
);
34 bool checkThrowEscape(Scope
*sc
, Expression
*e
, bool gag
);
35 LabelStatement
*checkLabeledLoop(Scope
*sc
, Statement
*statement
);
36 Identifier
*fixupLabelName(Scope
*sc
, Identifier
*ident
);
37 FuncDeclaration
*isFuncAddress(Expression
*e
, bool *hasOverloads
= NULL
);
38 VarDeclaration
*copyToTemp(StorageClass stc
, const char *name
, Expression
*e
);
39 Expression
*checkAssignmentAsCondition(Expression
*e
);
40 TypeIdentifier
*getThrowable();
42 Expression
*semantic(Expression
*e
, Scope
*sc
);
43 Statement
*semantic(Statement
*s
, Scope
*sc
);
44 void semantic(Catch
*c
, Scope
*sc
);
45 Statement
*semanticNoScope(Statement
*s
, Scope
*sc
);
46 Statement
*semanticScope(Statement
*s
, Scope
*sc
, Statement
*sbreak
, Statement
*scontinue
);
47 int blockExit(Statement
*s
, FuncDeclaration
*func
, bool mustNotThrow
);
49 class StatementSemanticVisitor
: public Visitor
55 StatementSemanticVisitor(Scope
*sc
)
64 result
= new ErrorStatement();
68 void visit(Statement
*s
)
73 void visit(ErrorStatement
*s
)
78 void visit(PeelStatement
*s
)
80 /* "peel" off this wrapper, and don't run semantic()
86 void visit(ExpStatement
*s
)
90 //printf("ExpStatement::semantic() %s\n", s->exp->toChars());
92 // Allow CommaExp in ExpStatement because return isn't used
93 if (s
->exp
->op
== TOKcomma
)
94 ((CommaExp
*)s
->exp
)->allowCommaExp
= true;
96 s
->exp
= semantic(s
->exp
, sc
);
97 s
->exp
= resolveProperties(sc
, s
->exp
);
98 s
->exp
= s
->exp
->addDtorHook(sc
);
99 if (checkNonAssignmentArrayOp(s
->exp
))
100 s
->exp
= new ErrorExp();
101 if (FuncDeclaration
*f
= isFuncAddress(s
->exp
))
103 if (f
->checkForwardRef(s
->exp
->loc
))
104 s
->exp
= new ErrorExp();
106 if (discardValue(s
->exp
))
107 s
->exp
= new ErrorExp();
109 s
->exp
= s
->exp
->optimize(WANTvalue
);
110 s
->exp
= checkGC(sc
, s
->exp
);
111 if (s
->exp
->op
== TOKerror
)
117 void visit(CompileStatement
*cs
)
119 //printf("CompileStatement::semantic() %s\n", cs->exp->toChars());
120 Statements
*a
= cs
->flatten(sc
);
123 Statement
*s
= new CompoundStatement(cs
->loc
, a
);
124 result
= semantic(s
, sc
);
127 void visit(CompoundStatement
*cs
)
129 //printf("CompoundStatement::semantic(this = %p, sc = %p)\n", cs, sc);
130 for (size_t i
= 0; i
< cs
->statements
->length
; )
132 Statement
*s
= (*cs
->statements
)[i
];
135 Statements
*flt
= s
->flatten(sc
);
138 cs
->statements
->remove(i
);
139 cs
->statements
->insert(i
, flt
);
143 (*cs
->statements
)[i
] = s
;
147 Statement
*sexception
;
150 (*cs
->statements
)[i
] = s
->scopeCode(sc
, &sentry
, &sexception
, &sfinally
);
153 sentry
= semantic(sentry
, sc
);
154 cs
->statements
->insert(i
, sentry
);
158 sexception
= semantic(sexception
, sc
);
161 if (i
+ 1 == cs
->statements
->length
&& !sfinally
)
171 * catch (Throwable __o)
172 * { sexception; throw __o; }
174 Statements
*a
= new Statements();
175 for (size_t j
= i
+ 1; j
< cs
->statements
->length
; j
++)
177 a
->push((*cs
->statements
)[j
]);
179 Statement
*body
= new CompoundStatement(Loc(), a
);
180 body
= new ScopeStatement(Loc(), body
, Loc());
182 Identifier
*id
= Identifier::generateId("__o");
184 Statement
*handler
= new PeelStatement(sexception
);
185 if (blockExit(sexception
, sc
->func
, false) & BEfallthru
)
187 ThrowStatement
*ts
= new ThrowStatement(Loc(), new IdentifierExp(Loc(), id
));
188 ts
->internalThrow
= true;
189 handler
= new CompoundStatement(Loc(), handler
, ts
);
192 Catches
*catches
= new Catches();
193 Catch
*ctch
= new Catch(Loc(), getThrowable(), id
, handler
);
194 ctch
->internalCatch
= true;
197 s
= new TryCatchStatement(Loc(), body
, catches
);
199 s
= new TryFinallyStatement(Loc(), s
, sfinally
);
202 cs
->statements
->setDim(i
+ 1);
203 cs
->statements
->push(s
);
209 if (0 && i
+ 1 == cs
->statements
->length
)
211 cs
->statements
->push(sfinally
);
218 * s; try { s1; s2; } finally { sfinally; }
220 Statements
*a
= new Statements();
221 for (size_t j
= i
+ 1; j
< cs
->statements
->length
; j
++)
223 a
->push((*cs
->statements
)[j
]);
225 Statement
*body
= new CompoundStatement(Loc(), a
);
226 s
= new TryFinallyStatement(Loc(), body
, sfinally
);
228 cs
->statements
->setDim(i
+ 1);
229 cs
->statements
->push(s
);
236 /* Remove NULL statements from the list.
238 cs
->statements
->remove(i
);
244 for (size_t i
= 0; i
< cs
->statements
->length
; ++i
)
247 Statement
*s
= (*cs
->statements
)[i
];
251 Statement
*se
= s
->isErrorStatement();
258 /* Bugzilla 11653: 'semantic' may return another CompoundStatement
259 * (eg. CaseRangeStatement), so flatten it here.
261 Statements
*flt
= s
->flatten(sc
);
264 cs
->statements
->remove(i
);
265 cs
->statements
->insert(i
, flt
);
266 if (cs
->statements
->length
<= i
)
271 if (cs
->statements
->length
== 1)
273 result
= (*cs
->statements
)[0];
279 void visit(UnrolledLoopStatement
*uls
)
281 //printf("UnrolledLoopStatement::semantic(this = %p, sc = %p)\n", uls, sc);
282 Scope
*scd
= sc
->push();
284 scd
->scontinue
= uls
;
286 Statement
*serror
= NULL
;
287 for (size_t i
= 0; i
< uls
->statements
->length
; i
++)
289 Statement
*s
= (*uls
->statements
)[i
];
292 //printf("[%d]: %s\n", i, s->toChars());
293 s
= semantic(s
, scd
);
294 (*uls
->statements
)[i
] = s
;
297 serror
= s
->isErrorStatement();
302 result
= serror
? serror
: uls
;
305 void visit(ScopeStatement
*ss
)
307 //printf("ScopeStatement::semantic(sc = %p)\n", sc);
310 ScopeDsymbol
*sym
= new ScopeDsymbol();
311 sym
->parent
= sc
->scopesym
;
312 sym
->endlinnum
= ss
->endloc
.linnum
;
315 Statements
*a
= ss
->statement
->flatten(sc
);
318 ss
->statement
= new CompoundStatement(ss
->loc
, a
);
321 ss
->statement
= semantic(ss
->statement
, sc
);
324 if (ss
->statement
->isErrorStatement())
327 result
= ss
->statement
;
332 Statement
*sexception
;
335 ss
->statement
= ss
->statement
->scopeCode(sc
, &sentry
, &sexception
, &sfinally
);
340 //printf("adding sfinally\n");
341 sfinally
= semantic(sfinally
, sc
);
342 ss
->statement
= new CompoundStatement(ss
->loc
, ss
->statement
, sfinally
);
351 void visit(ForwardingStatement
*ss
)
354 for (Scope
*csc
= sc
; !ss
->sym
->forward
; csc
= csc
->enclosing
)
357 ss
->sym
->forward
= csc
->scopesym
;
359 sc
= sc
->push(ss
->sym
);
362 ss
->statement
= semantic(ss
->statement
, sc
);
364 result
= ss
->statement
;
367 void visit(WhileStatement
*ws
)
369 /* Rewrite as a for(;condition;) loop
371 Statement
*s
= new ForStatement(ws
->loc
, NULL
, ws
->condition
, NULL
, ws
->_body
, ws
->endloc
);
376 void visit(DoStatement
*ds
)
380 ds
->_body
= semanticScope(ds
->_body
, sc
, ds
, ds
);
383 if (ds
->condition
->op
== TOKdotid
)
384 ((DotIdExp
*)ds
->condition
)->noderef
= true;
386 // check in syntax level
387 ds
->condition
= checkAssignmentAsCondition(ds
->condition
);
389 ds
->condition
= semantic(ds
->condition
, sc
);
390 ds
->condition
= resolveProperties(sc
, ds
->condition
);
391 if (checkNonAssignmentArrayOp(ds
->condition
))
392 ds
->condition
= new ErrorExp();
393 ds
->condition
= ds
->condition
->optimize(WANTvalue
);
394 ds
->condition
= checkGC(sc
, ds
->condition
);
396 ds
->condition
= ds
->condition
->toBoolean(sc
);
398 if (ds
->condition
->op
== TOKerror
)
401 if (ds
->_body
&& ds
->_body
->isErrorStatement())
410 void visit(ForStatement
*fs
)
412 //printf("ForStatement::semantic %s\n", toChars());
417 * for (auto v1 = i1, v2 = i2; condition; increment) { ... }
419 * { auto v1 = i1, v2 = i2; for (; condition; increment) { ... } }
425 * for (; condition; increment) { ... }
426 * } finally { v2.~this(); }
427 * } finally { v1.~this(); }
429 Statements
*ainit
= new Statements();
430 ainit
->push(fs
->_init
);
433 Statement
*s
= new CompoundStatement(fs
->loc
, ainit
);
434 s
= new ScopeStatement(fs
->loc
, s
, fs
->endloc
);
436 if (!s
->isErrorStatement())
438 if (LabelStatement
*ls
= checkLabeledLoop(sc
, fs
))
440 fs
->relatedLabeled
= s
;
445 assert(fs
->_init
== NULL
);
447 ScopeDsymbol
*sym
= new ScopeDsymbol();
448 sym
->parent
= sc
->scopesym
;
449 sym
->endlinnum
= fs
->endloc
.linnum
;
455 if (fs
->condition
->op
== TOKdotid
)
456 ((DotIdExp
*)fs
->condition
)->noderef
= true;
458 // check in syntax level
459 fs
->condition
= checkAssignmentAsCondition(fs
->condition
);
461 fs
->condition
= semantic(fs
->condition
, sc
);
462 fs
->condition
= resolveProperties(sc
, fs
->condition
);
463 if (checkNonAssignmentArrayOp(fs
->condition
))
464 fs
->condition
= new ErrorExp();
465 fs
->condition
= fs
->condition
->optimize(WANTvalue
);
466 fs
->condition
= checkGC(sc
, fs
->condition
);
467 fs
->condition
= fs
->condition
->toBoolean(sc
);
471 if (fs
->increment
->op
== TOKcomma
)
472 ((CommaExp
*)fs
->increment
)->allowCommaExp
= true;
473 fs
->increment
= semantic(fs
->increment
, sc
);
474 fs
->increment
= resolveProperties(sc
, fs
->increment
);
475 if (checkNonAssignmentArrayOp(fs
->increment
))
476 fs
->increment
= new ErrorExp();
477 fs
->increment
= fs
->increment
->optimize(WANTvalue
);
478 fs
->increment
= checkGC(sc
, fs
->increment
);
484 fs
->_body
= semanticNoScope(fs
->_body
, sc
);
489 if ((fs
->condition
&& fs
->condition
->op
== TOKerror
) ||
490 (fs
->increment
&& fs
->increment
->op
== TOKerror
) ||
491 (fs
->_body
&& fs
->_body
->isErrorStatement()))
497 /***********************
498 * Declares a unrolled `foreach` loop variable or a `static foreach` variable.
501 * storageClass = The storage class of the variable.
502 * type = The declared type of the variable.
503 * ident = The name of the variable.
504 * e = The initializer of the variable (i.e. the current element of the looped over aggregate).
505 * t = The type of the initializer.
507 * `true` iff the declaration was successful.
509 bool declareVariable(ForeachStatement
*fs
, Type
*paramtype
, TupleExp
*te
,
510 bool needExpansion
, bool isStatic
, Statements
*statements
, Dsymbols
*declarations
,
511 StorageClass storageClass
, Type
*type
, Identifier
*ident
, Expression
*e
, Type
*t
)
514 if (storageClass
& (STCout
| STClazy
) ||
515 (storageClass
& STCref
&& !te
))
517 fs
->error("no storage class for value %s", ident
->toChars());
523 Type
*tb
= e
->type
->toBasetype();
525 if (!(storageClass
& STCmanifest
))
527 if ((isStatic
|| tb
->ty
== Tfunction
|| tb
->ty
== Tsarray
|| storageClass
& STCalias
) && e
->op
== TOKvar
)
528 ds
= ((VarExp
*)e
)->var
;
529 else if (e
->op
== TOKtemplate
)
530 ds
= ((TemplateExp
*)e
)->td
;
531 else if (e
->op
== TOKscope
)
532 ds
= ((ScopeExp
*)e
)->sds
;
533 else if (e
->op
== TOKfunction
)
535 FuncExp
*fe
= (FuncExp
*)e
;
536 ds
= fe
->td
? (Dsymbol
*)fe
->td
: fe
->fd
;
539 else if (storageClass
& STCalias
)
541 fs
->error("foreach loop variable cannot be both enum and alias");
547 var
= new AliasDeclaration(loc
, ident
, ds
);
548 if (storageClass
& STCref
)
550 fs
->error("symbol %s cannot be ref", ds
->toChars());
555 fs
->error("cannot specify element type for symbol %s", ds
->toChars());
559 else if (e
->op
== TOKtype
)
561 var
= new AliasDeclaration(loc
, ident
, e
->type
);
564 fs
->error("cannot specify element type for type %s", e
->type
->toChars());
570 e
= resolveProperties(sc
, e
);
574 Initializer
*ie
= new ExpInitializer(Loc(), e
);
575 VarDeclaration
*v
= new VarDeclaration(loc
, type
, ident
, ie
);
576 if (storageClass
& STCref
)
577 v
->storage_class
|= STCref
| STCforeach
;
578 if (isStatic
|| storageClass
& STCmanifest
|| e
->isConst() ||
579 e
->op
== TOKstring
||
580 e
->op
== TOKstructliteral
||
581 e
->op
== TOKarrayliteral
)
583 if (v
->storage_class
& STCref
)
585 if (!isStatic
|| !needExpansion
)
587 fs
->error("constant value %s cannot be ref", ie
->toChars());
591 fs
->error("constant value %s cannot be ref", ident
->toChars());
596 v
->storage_class
|= STCmanifest
;
603 var
= new AliasDeclaration(loc
, ident
, t
);
606 fs
->error("cannot specify element type for symbol %s", fs
->toChars());
611 var
->storage_class
|= STClocal
;
613 statements
->push(new ExpStatement(loc
, var
));
614 else if (declarations
)
615 declarations
->push(var
);
621 bool makeTupleForeachBody(ForeachStatement
*fs
, size_t k
,
622 Type
*paramtype
, TupleExp
*te
, TypeTuple
*tuple
,
623 bool needExpansion
, bool isStatic
, bool isDecl
,
624 Statements
*statements
, Dsymbols
*declarations
, Dsymbols
*dbody
)
627 Expression
*e
= NULL
;
632 t
= Parameter::getNth(tuple
->arguments
, k
)->type
;
633 Parameter
*p
= (*fs
->parameters
)[0];
634 Statements
*stmts
= (isDecl
) ? NULL
: new Statements();
635 Dsymbols
*decls
= (isDecl
) ? new Dsymbols() : NULL
;
637 size_t dim
= fs
->parameters
->length
;
638 if (!needExpansion
&& dim
== 2)
641 if (p
->storageClass
& (STCout
| STCref
| STClazy
))
643 fs
->error("no storage class for key %s", p
->ident
->toChars());
650 p
->type
= Type::tsize_t
;
653 p
->type
= p
->type
->semantic(loc
, sc
);
654 TY keyty
= p
->type
->ty
;
655 if (keyty
!= Tint32
&& keyty
!= Tuns32
)
657 if (global
.params
.isLP64
)
659 if (keyty
!= Tint64
&& keyty
!= Tuns64
)
661 fs
->error("foreach: key type must be int or uint, long or ulong, not %s", p
->type
->toChars());
667 fs
->error("foreach: key type must be int or uint, not %s", p
->type
->toChars());
671 Initializer
*ie
= new ExpInitializer(Loc(), new IntegerExp(k
));
672 VarDeclaration
*var
= new VarDeclaration(loc
, p
->type
, p
->ident
, ie
);
673 var
->storage_class
|= STCmanifest
;
675 var
->storage_class
|= STClocal
;
677 stmts
->push(new ExpStatement(loc
, var
));
680 p
= (*fs
->parameters
)[1]; // value
683 if (!isStatic
|| !needExpansion
)
686 if (!declareVariable(fs
, paramtype
, te
, needExpansion
, isStatic
, stmts
, decls
,
687 p
->storageClass
, p
->type
, p
->ident
, e
, t
))
694 // expand tuples into multiple `static foreach` variables.
696 Identifier
*ident
= Identifier::generateId("__value");
697 declareVariable(fs
, paramtype
, te
, needExpansion
, isStatic
, stmts
, decls
,
698 0, e
->type
, ident
, e
, NULL
);
699 Identifier
*field
= Identifier::idPool("tuple");
700 Expression
*access
= new DotIdExp(loc
, e
, field
);
701 access
= semantic(access
, sc
);
704 //printf("%s\n", tuple->toChars());
705 for (size_t l
= 0; l
< dim
; l
++)
707 Parameter
*cp
= (*fs
->parameters
)[l
];
708 Expression
*init_
= new IndexExp(loc
, access
, new IntegerExp(loc
, l
, Type::tsize_t
));
709 init_
= semantic(init_
, sc
);
711 declareVariable(fs
, paramtype
, te
, needExpansion
, isStatic
, stmts
, decls
,
712 p
->storageClass
, init_
->type
, cp
->ident
, init_
, NULL
);
715 Statement
*fwdstmt
= NULL
;
716 Dsymbol
*fwddecl
= NULL
;
720 stmts
->push(fs
->_body
->syntaxCopy());
721 fwdstmt
= new CompoundStatement(loc
, stmts
);
725 decls
->append(Dsymbol::arraySyntaxCopy(dbody
));
729 fwdstmt
= new ScopeStatement(loc
, fwdstmt
, fs
->endloc
);
733 fwdstmt
= new ForwardingStatement(loc
, fwdstmt
);
737 fwddecl
= new ForwardingAttribDeclaration(decls
);
741 statements
->push(fwdstmt
);
742 else if (declarations
)
743 declarations
->push(fwddecl
);
750 * Type check and unroll `foreach` over an expression tuple as well
751 * as `static foreach` statements and `static foreach`
752 * declarations. For `static foreach` statements and `static
753 * foreach` declarations, the visitor interface is used (and the
754 * result is written into the `result` field.) For `static
755 * foreach` declarations, the resulting Dsymbols* are returned
758 * The unrolled body is wrapped into a
759 * - UnrolledLoopStatement, for `foreach` over an expression tuple.
760 * - ForwardingStatement, for `static foreach` statements.
761 * - ForwardingAttribDeclaration, for `static foreach` declarations.
763 * `static foreach` variables are declared as `STClocal`, such
764 * that they are inserted into the local symbol tables of the
765 * forwarding constructs instead of forwarded. For `static
766 * foreach` with multiple foreach loop variables whose aggregate
767 * has been lowered into a sequence of tuples, this function
768 * expands the tuples into multiple `STClocal` `static foreach`
771 bool makeTupleForeach(ForeachStatement
*fs
, bool needExpansion
, bool isStatic
, bool isDecl
,
772 Statements
*statements
, Dsymbols
*declarations
, Dsymbols
*dbody
)
775 size_t dim
= fs
->parameters
->length
;
776 if (!needExpansion
&& (dim
< 1 || dim
> 2))
778 fs
->error("only one (value) or two (key,value) arguments for tuple foreach");
782 Type
*paramtype
= (*fs
->parameters
)[dim
-1]->type
;
785 paramtype
= paramtype
->semantic(loc
, sc
);
786 if (paramtype
->ty
== Terror
)
790 Type
*tab
= fs
->aggr
->type
->toBasetype();
791 TypeTuple
*tuple
= (TypeTuple
*)tab
;
792 //printf("aggr: op = %d, %s\n", fs->aggr->op, fs->aggr->toChars());
795 if (fs
->aggr
->op
== TOKtuple
) // expression tuple
797 te
= (TupleExp
*)fs
->aggr
;
798 n
= te
->exps
->length
;
800 else if (fs
->aggr
->op
== TOKtype
) // type tuple
802 n
= Parameter::dim(tuple
->arguments
);
806 for (size_t j
= 0; j
< n
; j
++)
808 size_t k
= (fs
->op
== TOKforeach
) ? j
: n
- 1 - j
;
809 if (!makeTupleForeachBody(fs
, k
, paramtype
, te
, tuple
,
810 needExpansion
, isStatic
, isDecl
,
811 statements
, declarations
, dbody
))
817 Dsymbols
*makeTupleForeachStaticDecl(ForeachStatement
*fs
, Dsymbols
*dbody
, bool needExpansion
)
820 Dsymbols
*declarations
= new Dsymbols();
821 if (!makeTupleForeach(fs
, needExpansion
, true, true, NULL
, declarations
, dbody
))
827 void makeTupleForeachStatic(ForeachStatement
*fs
, bool needExpansion
)
831 Statements
*statements
= new Statements();
832 if (!makeTupleForeach(fs
, needExpansion
, true, false, statements
, NULL
, NULL
))
835 result
= new CompoundStatement(loc
, statements
);
838 void visit(ForeachStatement
*fs
)
840 //printf("ForeachStatement::semantic() %p\n", fs);
844 size_t dim
= fs
->parameters
->length
;
845 TypeAArray
*taa
= NULL
;
846 Dsymbol
*sapply
= NULL
;
853 fs
->func
= fs
->func
->fes
->func
;
855 VarDeclaration
*vinit
= NULL
;
856 fs
->aggr
= semantic(fs
->aggr
, sc
);
857 fs
->aggr
= resolveProperties(sc
, fs
->aggr
);
858 fs
->aggr
= fs
->aggr
->optimize(WANTvalue
);
859 if (fs
->aggr
->op
== TOKerror
)
862 Expression
*oaggr
= fs
->aggr
;
863 if (fs
->aggr
->type
&& fs
->aggr
->type
->toBasetype()->ty
== Tstruct
&&
864 ((TypeStruct
*)(fs
->aggr
->type
->toBasetype()))->sym
->dtor
&&
865 fs
->aggr
->op
!= TOKtype
&& !fs
->aggr
->isLvalue())
867 // Bugzilla 14653: Extend the life of rvalue aggregate till the end of foreach.
868 vinit
= copyToTemp(STCrvalue
, "__aggr", fs
->aggr
);
870 fs
->aggr
= new VarExp(fs
->aggr
->loc
, vinit
);
873 if (!inferAggregate(fs
, sc
, sapply
))
875 const char *msg
= "";
876 if (fs
->aggr
->type
&& isAggregate(fs
->aggr
->type
))
878 msg
= ", define opApply(), range primitives, or use .tupleof";
880 fs
->error("invalid foreach aggregate %s%s", oaggr
->toChars(), msg
);
884 Dsymbol
* sapplyOld
= sapply
; // 'sapply' will be NULL if and after 'inferApplyArgTypes' errors
886 /* Check for inference errors
888 if (!inferApplyArgTypes(fs
, sc
, sapply
))
891 Try and extract the parameter count of the opApply callback function, e.g.:
892 int opApply(int delegate(int, float)) => 2 args
894 bool foundMismatch
= false;
895 size_t foreachParamCount
= 0;
898 if (FuncDeclaration
*fd
= sapplyOld
->isFuncDeclaration())
900 int fvarargs
; // ignored (opApply shouldn't take variadics)
901 Parameters
*fparameters
= fd
->getParameters(&fvarargs
);
903 if (Parameter::dim(fparameters
) == 1)
905 // first param should be the callback function
906 Parameter
*fparam
= Parameter::getNth(fparameters
, 0);
907 if ((fparam
->type
->ty
== Tpointer
|| fparam
->type
->ty
== Tdelegate
) &&
908 fparam
->type
->nextOf()->ty
== Tfunction
)
910 TypeFunction
*tf
= (TypeFunction
*)fparam
->type
->nextOf();
911 foreachParamCount
= Parameter::dim(tf
->parameters
);
912 foundMismatch
= true;
918 //printf("dim = %d, parameters->length = %d\n", dim, fs->parameters->length);
919 if (foundMismatch
&& dim
!= foreachParamCount
)
921 const char *plural
= foreachParamCount
> 1 ? "s" : "";
922 fs
->error("cannot infer argument types, expected %d argument%s, not %d",
923 foreachParamCount
, plural
, dim
);
926 fs
->error("cannot uniquely infer foreach argument types");
931 Type
*tab
= fs
->aggr
->type
->toBasetype();
933 if (tab
->ty
== Ttuple
) // don't generate new scope for tuple loops
935 Statements
*statements
= new Statements();
936 if (!makeTupleForeach(fs
, false, false, false, statements
, NULL
, NULL
))
939 result
= new UnrolledLoopStatement(loc
, statements
);
940 if (LabelStatement
*ls
= checkLabeledLoop(sc
, fs
))
941 ls
->gotoTarget
= result
;
942 if (fs
->aggr
->op
== TOKtuple
)
944 TupleExp
*te
= (TupleExp
*)fs
->aggr
;
946 result
= new CompoundStatement(loc
, new ExpStatement(te
->e0
->loc
, te
->e0
), result
);
949 result
= new CompoundStatement(loc
, new ExpStatement(loc
, vinit
), result
);
950 result
= semantic(result
, sc
);
954 sym
= new ScopeDsymbol();
955 sym
->parent
= sc
->scopesym
;
956 sym
->endlinnum
= fs
->endloc
.linnum
;
957 Scope
*sc2
= sc
->push(sym
);
961 for (size_t i
= 0; i
< dim
; i
++)
963 Parameter
*p
= (*fs
->parameters
)[i
];
964 if (p
->storageClass
& STCmanifest
)
966 fs
->error("cannot declare enum loop variables for non-unrolled foreach");
968 if (p
->storageClass
& STCalias
)
970 fs
->error("cannot declare alias loop variables for non-unrolled foreach");
979 if (fs
->checkForArgTypes())
985 if (dim
< 1 || dim
> 2)
987 fs
->error("only one or two arguments for array foreach");
991 // Finish semantic on all foreach parameter types.
992 for (size_t i
= 0; i
< dim
; i
++)
994 Parameter
*p
= (*fs
->parameters
)[i
];
995 p
->type
= p
->type
->semantic(loc
, sc2
);
996 p
->type
= p
->type
->addStorageClass(p
->storageClass
);
999 tn
= tab
->nextOf()->toBasetype();
1003 Type
*tindex
= (*fs
->parameters
)[0]->type
;
1004 if (!tindex
->isintegral())
1006 fs
->error("foreach: key cannot be of non-integral type `%s`",
1010 /* What cases to deprecate implicit conversions for:
1011 * 1. foreach aggregate is a dynamic array
1012 * 2. foreach body is lowered to _aApply (see special case below).
1014 Type
*tv
= (*fs
->parameters
)[1]->type
->toBasetype();
1015 if ((tab
->ty
== Tarray
||
1016 (tn
->ty
!= tv
->ty
&&
1017 (tn
->ty
== Tchar
|| tn
->ty
== Twchar
|| tn
->ty
== Tdchar
) &&
1018 (tv
->ty
== Tchar
|| tv
->ty
== Twchar
|| tv
->ty
== Tdchar
))) &&
1019 !Type::tsize_t
->implicitConvTo(tindex
))
1021 fs
->deprecation("foreach: loop index implicitly converted from `size_t` to `%s`",
1026 /* Look for special case of parsing char types out of char type
1029 if (tn
->ty
== Tchar
|| tn
->ty
== Twchar
|| tn
->ty
== Tdchar
)
1031 int i
= (dim
== 1) ? 0 : 1; // index of value
1032 Parameter
*p
= (*fs
->parameters
)[i
];
1033 tnv
= p
->type
->toBasetype();
1034 if (tnv
->ty
!= tn
->ty
&&
1035 (tnv
->ty
== Tchar
|| tnv
->ty
== Twchar
|| tnv
->ty
== Tdchar
))
1037 if (p
->storageClass
& STCref
)
1039 fs
->error("foreach: value of UTF conversion cannot be ref");
1044 p
= (*fs
->parameters
)[0];
1045 if (p
->storageClass
& STCref
)
1047 fs
->error("foreach: key cannot be ref");
1055 for (size_t i
= 0; i
< dim
; i
++)
1057 // Declare parameterss
1058 Parameter
*p
= (*fs
->parameters
)[i
];
1059 VarDeclaration
*var
;
1061 if (dim
== 2 && i
== 0)
1063 var
= new VarDeclaration(loc
, p
->type
->mutableOf(), Identifier::generateId("__key"), NULL
);
1064 var
->storage_class
|= STCtemp
| STCforeach
;
1065 if (var
->storage_class
& (STCref
| STCout
))
1066 var
->storage_class
|= STCnodtor
;
1069 if (p
->storageClass
& STCref
)
1071 if (var
->type
->constConv(p
->type
) <= MATCHnomatch
)
1073 fs
->error("key type mismatch, %s to ref %s",
1074 var
->type
->toChars(), p
->type
->toChars());
1078 if (tab
->ty
== Tsarray
)
1080 TypeSArray
*ta
= (TypeSArray
*)tab
;
1081 IntRange dimrange
= getIntRange(ta
->dim
);
1082 if (!IntRange::fromType(var
->type
).contains(dimrange
))
1084 fs
->error("index type '%s' cannot cover index range 0..%llu", p
->type
->toChars(), ta
->dim
->toInteger());
1087 fs
->key
->range
= new IntRange(SignExtendedNumber(0), dimrange
.imax
);
1092 var
= new VarDeclaration(loc
, p
->type
, p
->ident
, NULL
);
1093 var
->storage_class
|= STCforeach
;
1094 var
->storage_class
|= p
->storageClass
& (STCin
| STCout
| STCref
| STC_TYPECTOR
);
1095 if (var
->storage_class
& (STCref
| STCout
))
1096 var
->storage_class
|= STCnodtor
;
1099 if (var
->storage_class
& STCref
)
1101 if (fs
->aggr
->checkModifiable(sc2
, 1) == 2)
1102 var
->storage_class
|= STCctorinit
;
1104 Type
*t
= tab
->nextOf();
1105 if (t
->constConv(p
->type
) <= MATCHnomatch
)
1107 fs
->error("argument type mismatch, %s to ref %s",
1108 t
->toChars(), p
->type
->toChars());
1115 /* Convert to a ForStatement
1116 * foreach (key, value; a) body =>
1117 * for (T[] tmp = a[], size_t key; key < tmp.length; ++key)
1118 * { T value = tmp[k]; body }
1120 * foreach_reverse (key, value; a) body =>
1121 * for (T[] tmp = a[], size_t key = tmp.length; key--; )
1122 * { T value = tmp[k]; body }
1124 Identifier
*id
= Identifier::generateId("__r");
1125 ExpInitializer
*ie
= new ExpInitializer(loc
, new SliceExp(loc
, fs
->aggr
, NULL
, NULL
));
1126 VarDeclaration
*tmp
;
1127 if (fs
->aggr
->op
== TOKarrayliteral
&&
1128 !((*fs
->parameters
)[dim
- 1]->storageClass
& STCref
))
1130 ArrayLiteralExp
*ale
= (ArrayLiteralExp
*)fs
->aggr
;
1131 size_t edim
= ale
->elements
? ale
->elements
->length
: 0;
1132 Type
*telem
= (*fs
->parameters
)[dim
- 1]->type
;
1134 // Bugzilla 12936: if telem has been specified explicitly,
1135 // converting array literal elements to telem might make it @nogc.
1136 fs
->aggr
= fs
->aggr
->implicitCastTo(sc
, telem
->sarrayOf(edim
));
1137 if (fs
->aggr
->op
== TOKerror
)
1140 // for (T[edim] tmp = a, ...)
1141 tmp
= new VarDeclaration(loc
, fs
->aggr
->type
, id
, ie
);
1144 tmp
= new VarDeclaration(loc
, tab
->nextOf()->arrayOf(), id
, ie
);
1145 tmp
->storage_class
|= STCtemp
;
1146 tmp
->endlinnum
= fs
->endloc
.linnum
;
1148 Expression
*tmp_length
= new DotIdExp(loc
, new VarExp(loc
, tmp
), Id::length
);
1152 Identifier
*idkey
= Identifier::generateId("__key");
1153 fs
->key
= new VarDeclaration(loc
, Type::tsize_t
, idkey
, NULL
);
1154 fs
->key
->storage_class
|= STCtemp
;
1156 else if (fs
->key
->type
->ty
!= Tsize_t
)
1158 tmp_length
= new CastExp(loc
, tmp_length
, fs
->key
->type
);
1160 if (fs
->op
== TOKforeach_reverse
)
1161 fs
->key
->_init
= new ExpInitializer(loc
, tmp_length
);
1163 fs
->key
->_init
= new ExpInitializer(loc
, new IntegerExp(loc
, 0, fs
->key
->type
));
1165 Statements
*cs
= new Statements();
1167 cs
->push(new ExpStatement(loc
, vinit
));
1168 cs
->push(new ExpStatement(loc
, tmp
));
1169 cs
->push(new ExpStatement(loc
, fs
->key
));
1170 Statement
*forinit
= new CompoundDeclarationStatement(loc
, cs
);
1173 if (fs
->op
== TOKforeach_reverse
)
1176 cond
= new PostExp(TOKminusminus
, loc
, new VarExp(loc
, fs
->key
));
1181 cond
= new CmpExp(TOKlt
, loc
, new VarExp(loc
, fs
->key
), tmp_length
);
1184 Expression
*increment
= NULL
;
1185 if (fs
->op
== TOKforeach
)
1188 increment
= new AddAssignExp(loc
, new VarExp(loc
, fs
->key
), new IntegerExp(loc
, 1, fs
->key
->type
));
1191 // T value = tmp[key];
1192 fs
->value
->_init
= new ExpInitializer(loc
, new IndexExp(loc
, new VarExp(loc
, tmp
), new VarExp(loc
, fs
->key
)));
1193 Statement
*ds
= new ExpStatement(loc
, fs
->value
);
1197 Parameter
*p
= (*fs
->parameters
)[0];
1198 if ((p
->storageClass
& STCref
) && p
->type
->equals(fs
->key
->type
))
1200 fs
->key
->range
= NULL
;
1201 AliasDeclaration
*v
= new AliasDeclaration(loc
, p
->ident
, fs
->key
);
1202 fs
->_body
= new CompoundStatement(loc
, new ExpStatement(loc
, v
), fs
->_body
);
1206 ExpInitializer
*ei
= new ExpInitializer(loc
, new IdentifierExp(loc
, fs
->key
->ident
));
1207 VarDeclaration
*v
= new VarDeclaration(loc
, p
->type
, p
->ident
, ei
);
1208 v
->storage_class
|= STCforeach
| (p
->storageClass
& STCref
);
1209 fs
->_body
= new CompoundStatement(loc
, new ExpStatement(loc
, v
), fs
->_body
);
1210 if (fs
->key
->range
&& !p
->type
->isMutable())
1212 /* Limit the range of the key to the specified range
1214 v
->range
= new IntRange(fs
->key
->range
->imin
, fs
->key
->range
->imax
- SignExtendedNumber(1));
1218 fs
->_body
= new CompoundStatement(loc
, ds
, fs
->_body
);
1220 s
= new ForStatement(loc
, forinit
, cond
, increment
, fs
->_body
, fs
->endloc
);
1221 if (LabelStatement
*ls
= checkLabeledLoop(sc
, fs
)) // Bugzilla 15450: don't use sc2
1223 s
= semantic(s
, sc2
);
1228 if (fs
->op
== TOKforeach_reverse
)
1229 fs
->warning("cannot use foreach_reverse with an associative array");
1230 if (fs
->checkForArgTypes())
1236 taa
= (TypeAArray
*)tab
;
1237 if (dim
< 1 || dim
> 2)
1239 fs
->error("only one or two arguments for associative array foreach");
1246 /* Prefer using opApply, if it exists
1252 /* Look for range iteration, i.e. the properties
1253 * .empty, .popFront, .popBack, .front and .back
1254 * foreach (e; aggr) { ... }
1256 * for (auto __r = aggr[]; !__r.empty; __r.popFront()) {
1257 * auto e = __r.front;
1261 AggregateDeclaration
*ad
= (tab
->ty
== Tclass
)
1262 ? (AggregateDeclaration
*)((TypeClass
*)tab
)->sym
1263 : (AggregateDeclaration
*)((TypeStruct
*)tab
)->sym
;
1264 Identifier
*idfront
;
1265 Identifier
*idpopFront
;
1266 if (fs
->op
== TOKforeach
)
1268 idfront
= Id::Ffront
;
1269 idpopFront
= Id::FpopFront
;
1273 idfront
= Id::Fback
;
1274 idpopFront
= Id::FpopBack
;
1276 Dsymbol
*sfront
= ad
->search(Loc(), idfront
);
1280 /* Generate a temporary __r and initialize it with the aggregate.
1284 if (vinit
&& fs
->aggr
->op
== TOKvar
&& ((VarExp
*)fs
->aggr
)->var
== vinit
)
1287 init
= new ExpStatement(loc
, vinit
);
1291 r
= copyToTemp(0, "__r", fs
->aggr
);
1293 init
= new ExpStatement(loc
, r
);
1295 init
= new CompoundStatement(loc
, new ExpStatement(loc
, vinit
), init
);
1299 Expression
*e
= new VarExp(loc
, r
);
1300 e
= new DotIdExp(loc
, e
, Id::Fempty
);
1301 Expression
*condition
= new NotExp(loc
, e
);
1304 e
= new VarExp(loc
, r
);
1305 Expression
*increment
= new CallExp(loc
, new DotIdExp(loc
, e
, idpopFront
));
1307 /* Declaration statement for e:
1308 * auto e = __r.idfront;
1310 e
= new VarExp(loc
, r
);
1311 Expression
*einit
= new DotIdExp(loc
, e
, idfront
);
1312 Statement
*makeargs
, *forbody
;
1315 Parameter
*p
= (*fs
->parameters
)[0];
1316 VarDeclaration
*ve
= new VarDeclaration(loc
, p
->type
, p
->ident
, new ExpInitializer(loc
, einit
));
1317 ve
->storage_class
|= STCforeach
;
1318 ve
->storage_class
|= p
->storageClass
& (STCin
| STCout
| STCref
| STC_TYPECTOR
);
1320 makeargs
= new ExpStatement(loc
, ve
);
1324 VarDeclaration
*vd
= copyToTemp(STCref
, "__front", einit
);
1326 makeargs
= new ExpStatement(loc
, vd
);
1328 Type
*tfront
= NULL
;
1329 if (FuncDeclaration
*fd
= sfront
->isFuncDeclaration())
1331 if (!fd
->functionSemantic())
1335 else if (TemplateDeclaration
*td
= sfront
->isTemplateDeclaration())
1338 if (FuncDeclaration
*f
= resolveFuncCall(loc
, sc
, td
, NULL
, tab
, &a
, 1))
1341 else if (Declaration
*d
= sfront
->isDeclaration())
1345 if (!tfront
|| tfront
->ty
== Terror
)
1348 if (tfront
->toBasetype()->ty
== Tfunction
)
1349 tfront
= tfront
->toBasetype()->nextOf();
1350 if (tfront
->ty
== Tvoid
)
1352 fs
->error("%s.front is void and has no value", oaggr
->toChars());
1356 // Resolve inout qualifier of front type
1357 tfront
= tfront
->substWildTo(tab
->mod
);
1359 Expression
*ve
= new VarExp(loc
, vd
);
1362 Expressions
*exps
= new Expressions();
1365 while (exps
->length
< dim
)
1367 pos
= expandAliasThisTuples(exps
, pos
);
1371 if (exps
->length
!= dim
)
1373 const char *plural
= exps
->length
> 1 ? "s" : "";
1374 fs
->error("cannot infer argument types, expected %d argument%s, not %d",
1375 exps
->length
, plural
, dim
);
1379 for (size_t i
= 0; i
< dim
; i
++)
1381 Parameter
*p
= (*fs
->parameters
)[i
];
1382 Expression
*exp
= (*exps
)[i
];
1384 p
->type
= exp
->type
;
1385 p
->type
= p
->type
->addStorageClass(p
->storageClass
)->semantic(loc
, sc2
);
1386 if (!exp
->implicitConvTo(p
->type
))
1389 VarDeclaration
*var
= new VarDeclaration(loc
, p
->type
, p
->ident
, new ExpInitializer(loc
, exp
));
1390 var
->storage_class
|= STCctfe
| STCref
| STCforeach
;
1391 makeargs
= new CompoundStatement(loc
, makeargs
, new ExpStatement(loc
, var
));
1396 forbody
= new CompoundStatement(loc
,
1397 makeargs
, fs
->_body
);
1399 s
= new ForStatement(loc
, init
, condition
, increment
, forbody
, fs
->endloc
);
1400 if (LabelStatement
*ls
= checkLabeledLoop(sc
, fs
))
1402 s
= semantic(s
, sc2
);
1406 fs
->error("cannot infer argument types");
1410 if (fs
->op
== TOKforeach_reverse
)
1411 fs
->deprecation("cannot use foreach_reverse with a delegate");
1414 if (fs
->checkForArgTypes())
1416 fs
->_body
= semanticNoScope(fs
->_body
, sc2
);
1421 TypeFunction
*tfld
= NULL
;
1424 FuncDeclaration
*fdapply
= sapply
->isFuncDeclaration();
1427 assert(fdapply
->type
&& fdapply
->type
->ty
== Tfunction
);
1428 tfld
= (TypeFunction
*)fdapply
->type
->semantic(loc
, sc2
);
1431 else if (tab
->ty
== Tdelegate
)
1433 tfld
= (TypeFunction
*)tab
->nextOf();
1435 //printf("tfld = %s\n", tfld->toChars());
1436 if (tfld
->parameters
->length
== 1)
1438 Parameter
*p
= Parameter::getNth(tfld
->parameters
, 0);
1439 if (p
->type
&& p
->type
->ty
== Tdelegate
)
1441 Type
*t
= p
->type
->semantic(loc
, sc2
);
1442 assert(t
->ty
== Tdelegate
);
1443 tfld
= (TypeFunction
*)t
->nextOf();
1449 /* Turn body into the function literal:
1450 * int delegate(ref T param) { body }
1452 Parameters
*params
= new Parameters();
1453 for (size_t i
= 0; i
< dim
; i
++)
1455 Parameter
*p
= (*fs
->parameters
)[i
];
1456 StorageClass stc
= STCref
;
1459 p
->type
= p
->type
->semantic(loc
, sc2
);
1460 p
->type
= p
->type
->addStorageClass(p
->storageClass
);
1463 Parameter
*prm
= Parameter::getNth(tfld
->parameters
, i
);
1464 //printf("\tprm = %s%s\n", (prm->storageClass&STCref?"ref ":""), prm->ident->toChars());
1465 stc
= prm
->storageClass
& STCref
;
1466 id
= p
->ident
; // argument copy is not need.
1467 if ((p
->storageClass
& STCref
) != stc
)
1471 fs
->error("foreach: cannot make %s ref", p
->ident
->toChars());
1477 else if (p
->storageClass
& STCref
)
1479 // default delegate parameters are marked as ref, then
1480 // argument copy is not need.
1485 // Make a copy of the ref argument so it isn't
1488 id
= Identifier::generateId("__applyArg", (int)i
);
1490 Initializer
*ie
= new ExpInitializer(Loc(), new IdentifierExp(Loc(), id
));
1491 VarDeclaration
*v
= new VarDeclaration(Loc(), p
->type
, p
->ident
, ie
);
1492 v
->storage_class
|= STCtemp
;
1493 s
= new ExpStatement(Loc(), v
);
1494 fs
->_body
= new CompoundStatement(loc
, s
, fs
->_body
);
1496 params
->push(new Parameter(stc
, p
->type
, id
, NULL
));
1498 // Bugzilla 13840: Throwable nested function inside nothrow function is acceptable.
1499 StorageClass stc
= mergeFuncAttrs(STCsafe
| STCpure
| STCnogc
, fs
->func
);
1500 tfld
= new TypeFunction(params
, Type::tint32
, 0, LINKd
, stc
);
1501 fs
->cases
= new Statements();
1502 fs
->gotos
= new ScopeStatements();
1503 FuncLiteralDeclaration
*fld
= new FuncLiteralDeclaration(loc
, Loc(), tfld
, TOKdelegate
, fs
);
1504 fld
->fbody
= fs
->_body
;
1505 Expression
*flde
= new FuncExp(loc
, fld
);
1506 flde
= semantic(flde
, sc2
);
1507 fld
->tookAddressOf
= 0;
1509 // Resolve any forward referenced goto's
1510 for (size_t i
= 0; i
< fs
->gotos
->length
; i
++)
1512 GotoStatement
*gs
= (GotoStatement
*)(*fs
->gotos
)[i
]->statement
;
1513 if (!gs
->label
->statement
)
1515 // 'Promote' it to this scope, and replace with a return
1516 fs
->cases
->push(gs
);
1517 s
= new ReturnStatement(Loc(), new IntegerExp(fs
->cases
->length
+ 1));
1518 (*fs
->gotos
)[i
]->statement
= s
;
1522 Expression
*e
= NULL
;
1526 e
= new DeclarationExp(loc
, vinit
);
1527 e
= semantic(e
, sc2
);
1528 if (e
->op
== TOKerror
)
1535 Parameter
*p
= (*fs
->parameters
)[0];
1536 bool isRef
= (p
->storageClass
& STCref
) != 0;
1540 Type
*ti
= (isRef
? taa
->index
->addMod(MODconst
) : taa
->index
);
1541 if (isRef
? !ti
->constConv(ta
) : !ti
->implicitConvTo(ta
))
1543 fs
->error("foreach: index must be type %s, not %s", ti
->toChars(), ta
->toChars());
1546 p
= (*fs
->parameters
)[1];
1547 isRef
= (p
->storageClass
& STCref
) != 0;
1550 Type
*taav
= taa
->nextOf();
1551 if (isRef
? !taav
->constConv(ta
) : !taav
->implicitConvTo(ta
))
1553 fs
->error("foreach: value must be type %s, not %s", taav
->toChars(), ta
->toChars());
1558 * extern(C) int _aaApply(void*, in size_t, int delegate(void*))
1559 * _aaApply(aggr, keysize, flde)
1561 * extern(C) int _aaApply2(void*, in size_t, int delegate(void*, void*))
1562 * _aaApply2(aggr, keysize, flde)
1564 static const char *name
[2] = { "_aaApply", "_aaApply2" };
1565 static FuncDeclaration
*fdapply
[2] = { NULL
, NULL
};
1566 static TypeDelegate
*fldeTy
[2] = { NULL
, NULL
};
1568 unsigned char i
= (dim
== 2 ? 1 : 0);
1571 params
= new Parameters();
1572 params
->push(new Parameter(0, Type::tvoid
->pointerTo(), NULL
, NULL
));
1573 params
->push(new Parameter(STCin
, Type::tsize_t
, NULL
, NULL
));
1574 Parameters
* dgparams
= new Parameters
;
1575 dgparams
->push(new Parameter(0, Type::tvoidptr
, NULL
, NULL
));
1577 dgparams
->push(new Parameter(0, Type::tvoidptr
, NULL
, NULL
));
1578 fldeTy
[i
] = new TypeDelegate(new TypeFunction(dgparams
, Type::tint32
, 0, LINKd
));
1579 params
->push(new Parameter(0, fldeTy
[i
], NULL
, NULL
));
1580 fdapply
[i
] = FuncDeclaration::genCfunc(params
, Type::tint32
, name
[i
]);
1583 Expressions
*exps
= new Expressions();
1584 exps
->push(fs
->aggr
);
1585 d_uns64 keysize
= taa
->index
->size();
1586 if (keysize
== SIZE_INVALID
)
1588 assert(keysize
< UINT64_MAX
- Target::ptrsize
);
1589 keysize
= (keysize
+ (Target::ptrsize
- 1)) & ~(Target::ptrsize
- 1);
1590 // paint delegate argument to the type runtime expects
1591 if (!fldeTy
[i
]->equals(flde
->type
))
1593 flde
= new CastExp(loc
, flde
, flde
->type
);
1594 flde
->type
= fldeTy
[i
];
1596 exps
->push(new IntegerExp(Loc(), keysize
, Type::tsize_t
));
1599 ec
= new VarExp(Loc(), fdapply
[i
], false);
1600 ec
= new CallExp(loc
, ec
, exps
);
1601 ec
->type
= Type::tint32
; // don't run semantic() on ec
1603 else if (tab
->ty
== Tarray
|| tab
->ty
== Tsarray
)
1606 * _aApply(aggr, flde)
1608 static const char fntab
[9][3] =
1613 const int BUFFER_LEN
= 7+1+2+ sizeof(dim
)*3 + 1;
1614 char fdname
[BUFFER_LEN
];
1619 case Tchar
: flag
= 0; break;
1620 case Twchar
: flag
= 3; break;
1621 case Tdchar
: flag
= 6; break;
1626 case Tchar
: flag
+= 0; break;
1627 case Twchar
: flag
+= 1; break;
1628 case Tdchar
: flag
+= 2; break;
1631 const char *r
= (fs
->op
== TOKforeach_reverse
) ? "R" : "";
1632 int j
= sprintf(fdname
, "_aApply%s%.*s%llu", r
, 2, fntab
[flag
], (ulonglong
)dim
);
1633 assert(j
< BUFFER_LEN
);
1635 FuncDeclaration
*fdapply
;
1637 params
= new Parameters();
1638 params
->push(new Parameter(STCin
, tn
->arrayOf(), NULL
, NULL
));
1639 Parameters
* dgparams
= new Parameters
;
1640 dgparams
->push(new Parameter(0, Type::tvoidptr
, NULL
, NULL
));
1642 dgparams
->push(new Parameter(0, Type::tvoidptr
, NULL
, NULL
));
1643 dgty
= new TypeDelegate(new TypeFunction(dgparams
, Type::tint32
, 0, LINKd
));
1644 params
->push(new Parameter(0, dgty
, NULL
, NULL
));
1645 fdapply
= FuncDeclaration::genCfunc(params
, Type::tint32
, fdname
);
1647 if (tab
->ty
== Tsarray
)
1648 fs
->aggr
= fs
->aggr
->castTo(sc2
, tn
->arrayOf());
1650 // paint delegate argument to the type runtime expects
1651 if (!dgty
->equals(flde
->type
)) {
1652 flde
= new CastExp(loc
, flde
, flde
->type
);
1656 ec
= new VarExp(Loc(), fdapply
, false);
1657 ec
= new CallExp(loc
, ec
, fs
->aggr
, flde
);
1658 ec
->type
= Type::tint32
; // don't run semantic() on ec
1660 else if (tab
->ty
== Tdelegate
)
1665 if (fs
->aggr
->op
== TOKdelegate
&&
1666 ((DelegateExp
*)fs
->aggr
)->func
->isNested())
1668 // See Bugzilla 3560
1669 fs
->aggr
= ((DelegateExp
*)fs
->aggr
)->e1
;
1671 ec
= new CallExp(loc
, fs
->aggr
, flde
);
1672 ec
= semantic(ec
, sc2
);
1673 if (ec
->op
== TOKerror
)
1675 if (ec
->type
!= Type::tint32
)
1677 fs
->error("opApply() function for %s must return an int", tab
->toChars());
1683 if (global
.params
.vsafe
)
1684 fld
->tookAddressOf
= 1; // allocate a closure unless the opApply() uses 'scope'
1686 assert(tab
->ty
== Tstruct
|| tab
->ty
== Tclass
);
1691 ec
= new DotIdExp(loc
, fs
->aggr
, sapply
->ident
);
1692 ec
= new CallExp(loc
, ec
, flde
);
1693 ec
= semantic(ec
, sc2
);
1694 if (ec
->op
== TOKerror
)
1696 if (ec
->type
!= Type::tint32
)
1698 fs
->error("opApply() function for %s must return an int", tab
->toChars());
1702 e
= Expression::combine(e
, ec
);
1704 if (!fs
->cases
->length
)
1706 // Easy case, a clean exit from the loop
1707 e
= new CastExp(loc
, e
, Type::tvoid
); // Bugzilla 13899
1708 s
= new ExpStatement(loc
, e
);
1712 // Construct a switch statement around the return value
1713 // of the apply function.
1714 Statements
*a
= new Statements();
1716 // default: break; takes care of cases 0 and 1
1717 s
= new BreakStatement(Loc(), NULL
);
1718 s
= new DefaultStatement(Loc(), s
);
1722 for (size_t i
= 0; i
< fs
->cases
->length
; i
++)
1724 s
= (*fs
->cases
)[i
];
1725 s
= new CaseStatement(Loc(), new IntegerExp(i
+ 2), s
);
1729 s
= new CompoundStatement(loc
, a
);
1730 s
= new SwitchStatement(loc
, e
, s
, false);
1732 s
= semantic(s
, sc2
);
1737 s
= new ErrorStatement();
1741 fs
->error("foreach: %s is not an aggregate type", fs
->aggr
->type
->toChars());
1749 void visit(ForeachRangeStatement
*fs
)
1751 //printf("ForeachRangeStatement::semantic() %p\n", fs);
1753 fs
->lwr
= semantic(fs
->lwr
, sc
);
1754 fs
->lwr
= resolveProperties(sc
, fs
->lwr
);
1755 fs
->lwr
= fs
->lwr
->optimize(WANTvalue
);
1758 fs
->error("invalid range lower bound %s", fs
->lwr
->toChars());
1763 fs
->upr
= semantic(fs
->upr
, sc
);
1764 fs
->upr
= resolveProperties(sc
, fs
->upr
);
1765 fs
->upr
= fs
->upr
->optimize(WANTvalue
);
1768 fs
->error("invalid range upper bound %s", fs
->upr
->toChars());
1774 fs
->prm
->type
= fs
->prm
->type
->semantic(loc
, sc
);
1775 fs
->prm
->type
= fs
->prm
->type
->addStorageClass(fs
->prm
->storageClass
);
1776 fs
->lwr
= fs
->lwr
->implicitCastTo(sc
, fs
->prm
->type
);
1778 if (fs
->upr
->implicitConvTo(fs
->prm
->type
) || (fs
->prm
->storageClass
& STCref
))
1780 fs
->upr
= fs
->upr
->implicitCastTo(sc
, fs
->prm
->type
);
1784 // See if upr-1 fits in prm->type
1785 Expression
*limit
= new MinExp(loc
, fs
->upr
, new IntegerExp(1));
1786 limit
= semantic(limit
, sc
);
1787 limit
= limit
->optimize(WANTvalue
);
1788 if (!limit
->implicitConvTo(fs
->prm
->type
))
1790 fs
->upr
= fs
->upr
->implicitCastTo(sc
, fs
->prm
->type
);
1796 /* Must infer types from lwr and upr
1798 Type
*tlwr
= fs
->lwr
->type
->toBasetype();
1799 if (tlwr
->ty
== Tstruct
|| tlwr
->ty
== Tclass
)
1801 /* Just picking the first really isn't good enough.
1803 fs
->prm
->type
= fs
->lwr
->type
;
1805 else if (fs
->lwr
->type
== fs
->upr
->type
)
1807 /* Same logic as CondExp ?lwr:upr
1809 fs
->prm
->type
= fs
->lwr
->type
;
1813 AddExp
ea(loc
, fs
->lwr
, fs
->upr
);
1814 if (typeCombine(&ea
, sc
))
1816 fs
->prm
->type
= ea
.type
;
1820 fs
->prm
->type
= fs
->prm
->type
->addStorageClass(fs
->prm
->storageClass
);
1822 if (fs
->prm
->type
->ty
== Terror
||
1823 fs
->lwr
->op
== TOKerror
||
1824 fs
->upr
->op
== TOKerror
)
1829 /* Convert to a for loop:
1830 * foreach (key; lwr .. upr) =>
1831 * for (auto key = lwr, auto tmp = upr; key < tmp; ++key)
1833 * foreach_reverse (key; lwr .. upr) =>
1834 * for (auto tmp = lwr, auto key = upr; key-- > tmp;)
1836 ExpInitializer
*ie
= new ExpInitializer(loc
, (fs
->op
== TOKforeach
) ? fs
->lwr
: fs
->upr
);
1837 fs
->key
= new VarDeclaration(loc
, fs
->upr
->type
->mutableOf(), Identifier::generateId("__key"), ie
);
1838 fs
->key
->storage_class
|= STCtemp
;
1839 SignExtendedNumber lower
= getIntRange(fs
->lwr
).imin
;
1840 SignExtendedNumber upper
= getIntRange(fs
->upr
).imax
;
1843 fs
->key
->range
= new IntRange(lower
, upper
);
1846 Identifier
*id
= Identifier::generateId("__limit");
1847 ie
= new ExpInitializer(loc
, (fs
->op
== TOKforeach
) ? fs
->upr
: fs
->lwr
);
1848 VarDeclaration
*tmp
= new VarDeclaration(loc
, fs
->upr
->type
, id
, ie
);
1849 tmp
->storage_class
|= STCtemp
;
1851 Statements
*cs
= new Statements();
1852 // Keep order of evaluation as lwr, then upr
1853 if (fs
->op
== TOKforeach
)
1855 cs
->push(new ExpStatement(loc
, fs
->key
));
1856 cs
->push(new ExpStatement(loc
, tmp
));
1860 cs
->push(new ExpStatement(loc
, tmp
));
1861 cs
->push(new ExpStatement(loc
, fs
->key
));
1863 Statement
*forinit
= new CompoundDeclarationStatement(loc
, cs
);
1866 if (fs
->op
== TOKforeach_reverse
)
1868 cond
= new PostExp(TOKminusminus
, loc
, new VarExp(loc
, fs
->key
));
1869 if (fs
->prm
->type
->isscalar())
1872 cond
= new CmpExp(TOKgt
, loc
, cond
, new VarExp(loc
, tmp
));
1877 cond
= new EqualExp(TOKnotequal
, loc
, cond
, new VarExp(loc
, tmp
));
1882 if (fs
->prm
->type
->isscalar())
1885 cond
= new CmpExp(TOKlt
, loc
, new VarExp(loc
, fs
->key
), new VarExp(loc
, tmp
));
1890 cond
= new EqualExp(TOKnotequal
, loc
, new VarExp(loc
, fs
->key
), new VarExp(loc
, tmp
));
1894 Expression
*increment
= NULL
;
1895 if (fs
->op
== TOKforeach
)
1898 //increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1));
1899 increment
= new PreExp(TOKpreplusplus
, loc
, new VarExp(loc
, fs
->key
));
1902 if ((fs
->prm
->storageClass
& STCref
) && fs
->prm
->type
->equals(fs
->key
->type
))
1904 fs
->key
->range
= NULL
;
1905 AliasDeclaration
*v
= new AliasDeclaration(loc
, fs
->prm
->ident
, fs
->key
);
1906 fs
->_body
= new CompoundStatement(loc
, new ExpStatement(loc
, v
), fs
->_body
);
1910 ie
= new ExpInitializer(loc
, new CastExp(loc
, new VarExp(loc
, fs
->key
), fs
->prm
->type
));
1911 VarDeclaration
*v
= new VarDeclaration(loc
, fs
->prm
->type
, fs
->prm
->ident
, ie
);
1912 v
->storage_class
|= STCtemp
| STCforeach
| (fs
->prm
->storageClass
& STCref
);
1913 fs
->_body
= new CompoundStatement(loc
, new ExpStatement(loc
, v
), fs
->_body
);
1914 if (fs
->key
->range
&& !fs
->prm
->type
->isMutable())
1916 /* Limit the range of the key to the specified range
1918 v
->range
= new IntRange(fs
->key
->range
->imin
, fs
->key
->range
->imax
- SignExtendedNumber(1));
1921 if (fs
->prm
->storageClass
& STCref
)
1923 if (fs
->key
->type
->constConv(fs
->prm
->type
) <= MATCHnomatch
)
1925 fs
->error("prmument type mismatch, %s to ref %s",
1926 fs
->key
->type
->toChars(), fs
->prm
->type
->toChars());
1931 ForStatement
*s
= new ForStatement(loc
, forinit
, cond
, increment
, fs
->_body
, fs
->endloc
);
1932 if (LabelStatement
*ls
= checkLabeledLoop(sc
, fs
))
1934 result
= semantic(s
, sc
);
1937 void visit(IfStatement
*ifs
)
1939 // Evaluate at runtime
1940 unsigned cs0
= sc
->callSuper
;
1942 unsigned *fi0
= sc
->saveFieldInit();
1943 unsigned *fi1
= NULL
;
1945 // check in syntax level
1946 ifs
->condition
= checkAssignmentAsCondition(ifs
->condition
);
1948 ScopeDsymbol
*sym
= new ScopeDsymbol();
1949 sym
->parent
= sc
->scopesym
;
1950 sym
->endlinnum
= ifs
->endloc
.linnum
;
1951 Scope
*scd
= sc
->push(sym
);
1954 /* Declare prm, which we will set to be the
1955 * result of condition.
1957 ExpInitializer
*ei
= new ExpInitializer(ifs
->loc
, ifs
->condition
);
1958 ifs
->match
= new VarDeclaration(ifs
->loc
, ifs
->prm
->type
, ifs
->prm
->ident
, ei
);
1959 ifs
->match
->parent
= sc
->func
;
1960 ifs
->match
->storage_class
|= ifs
->prm
->storageClass
;
1961 ifs
->match
->semantic(scd
);
1963 DeclarationExp
*de
= new DeclarationExp(ifs
->loc
, ifs
->match
);
1964 VarExp
*ve
= new VarExp(ifs
->loc
, ifs
->match
);
1965 ifs
->condition
= new CommaExp(ifs
->loc
, de
, ve
);
1966 ifs
->condition
= semantic(ifs
->condition
, scd
);
1968 if (ifs
->match
->edtor
)
1970 Statement
*sdtor
= new DtorExpStatement(ifs
->loc
, ifs
->match
->edtor
, ifs
->match
);
1971 sdtor
= new OnScopeStatement(ifs
->loc
, TOKon_scope_exit
, sdtor
);
1972 ifs
->ifbody
= new CompoundStatement(ifs
->loc
, sdtor
, ifs
->ifbody
);
1973 ifs
->match
->storage_class
|= STCnodtor
;
1978 if (ifs
->condition
->op
== TOKdotid
)
1979 ((DotIdExp
*)ifs
->condition
)->noderef
= true;
1981 ifs
->condition
= semantic(ifs
->condition
, sc
);
1982 ifs
->condition
= resolveProperties(sc
, ifs
->condition
);
1983 ifs
->condition
= ifs
->condition
->addDtorHook(sc
);
1985 if (checkNonAssignmentArrayOp(ifs
->condition
))
1986 ifs
->condition
= new ErrorExp();
1987 ifs
->condition
= checkGC(sc
, ifs
->condition
);
1989 // Convert to boolean after declaring prm so this works:
1990 // if (S prm = S()) {}
1991 // where S is a struct that defines opCast!bool.
1992 ifs
->condition
= ifs
->condition
->toBoolean(sc
);
1994 // If we can short-circuit evaluate the if statement, don't do the
1995 // semantic analysis of the skipped code.
1996 // This feature allows a limited form of conditional compilation.
1997 ifs
->condition
= ifs
->condition
->optimize(WANTvalue
);
1998 ifs
->ifbody
= semanticNoScope(ifs
->ifbody
, scd
);
2001 cs1
= sc
->callSuper
;
2002 fi1
= sc
->fieldinit
;
2003 sc
->callSuper
= cs0
;
2004 sc
->fieldinit
= fi0
;
2006 ifs
->elsebody
= semanticScope(ifs
->elsebody
, sc
, NULL
, NULL
);
2007 sc
->mergeCallSuper(ifs
->loc
, cs1
);
2008 sc
->mergeFieldInit(ifs
->loc
, fi1
);
2010 if (ifs
->condition
->op
== TOKerror
||
2011 (ifs
->ifbody
&& ifs
->ifbody
->isErrorStatement()) ||
2012 (ifs
->elsebody
&& ifs
->elsebody
->isErrorStatement()))
2019 void visit(ConditionalStatement
*cs
)
2021 //printf("ConditionalStatement::semantic()\n");
2023 // If we can short-circuit evaluate the if statement, don't do the
2024 // semantic analysis of the skipped code.
2025 // This feature allows a limited form of conditional compilation.
2026 if (cs
->condition
->include(sc
, NULL
))
2028 DebugCondition
*dc
= cs
->condition
->isDebugCondition();
2032 sc
->flags
|= SCOPEdebug
;
2033 cs
->ifbody
= semantic(cs
->ifbody
, sc
);
2037 cs
->ifbody
= semantic(cs
->ifbody
, sc
);
2038 result
= cs
->ifbody
;
2043 cs
->elsebody
= semantic(cs
->elsebody
, sc
);
2044 result
= cs
->elsebody
;
2048 void visit(PragmaStatement
*ps
)
2050 // Should be merged with PragmaDeclaration
2051 //printf("PragmaStatement::semantic() %s\n", ps->toChars());
2052 //printf("body = %p\n", ps->_body);
2053 if (ps
->ident
== Id::msg
)
2057 for (size_t i
= 0; i
< ps
->args
->length
; i
++)
2059 Expression
*e
= (*ps
->args
)[i
];
2061 sc
= sc
->startCTFE();
2062 e
= semantic(e
, sc
);
2063 e
= resolveProperties(sc
, e
);
2065 // pragma(msg) is allowed to contain types as well as expressions
2066 e
= ctfeInterpretForPragmaMsg(e
);
2067 if (e
->op
== TOKerror
)
2069 errorSupplemental(ps
->loc
, "while evaluating pragma(msg, %s)", (*ps
->args
)[i
]->toChars());
2072 StringExp
*se
= e
->toStringExp();
2075 se
= se
->toUTF8(sc
);
2076 fprintf(stderr
, "%.*s", (int)se
->len
, (char *)se
->string
);
2079 fprintf(stderr
, "%s", e
->toChars());
2081 fprintf(stderr
, "\n");
2084 else if (ps
->ident
== Id::lib
)
2086 /* Should this be allowed?
2088 ps
->error("pragma(lib) not allowed as statement");
2091 else if (ps
->ident
== Id::startaddress
)
2093 if (!ps
->args
|| ps
->args
->length
!= 1)
2094 ps
->error("function name expected for start address");
2097 Expression
*e
= (*ps
->args
)[0];
2099 sc
= sc
->startCTFE();
2100 e
= semantic(e
, sc
);
2101 e
= resolveProperties(sc
, e
);
2104 e
= e
->ctfeInterpret();
2106 Dsymbol
*sa
= getDsymbol(e
);
2107 if (!sa
|| !sa
->isFuncDeclaration())
2109 ps
->error("function name expected for start address, not '%s'", e
->toChars());
2114 ps
->_body
= semantic(ps
->_body
, sc
);
2115 if (ps
->_body
->isErrorStatement())
2125 else if (ps
->ident
== Id::Pinline
)
2127 PINLINE inlining
= PINLINEdefault
;
2128 if (!ps
->args
|| ps
->args
->length
== 0)
2129 inlining
= PINLINEdefault
;
2130 else if (!ps
->args
|| ps
->args
->length
!= 1)
2132 ps
->error("boolean expression expected for pragma(inline)");
2137 Expression
*e
= (*ps
->args
)[0];
2139 if (e
->op
!= TOKint64
|| !e
->type
->equals(Type::tbool
))
2141 ps
->error("pragma(inline, true or false) expected, not %s", e
->toChars());
2145 if (e
->isBool(true))
2146 inlining
= PINLINEalways
;
2147 else if (e
->isBool(false))
2148 inlining
= PINLINEnever
;
2150 FuncDeclaration
*fd
= sc
->func
;
2153 ps
->error("pragma(inline) is not inside a function");
2156 fd
->inlining
= inlining
;
2161 ps
->error("unrecognized pragma(%s)", ps
->ident
->toChars());
2167 if (ps
->ident
== Id::msg
|| ps
->ident
== Id::startaddress
)
2169 ps
->error("`pragma(%s)` is missing a terminating `;`", ps
->ident
->toChars());
2172 ps
->_body
= semantic(ps
->_body
, sc
);
2181 void visit(StaticAssertStatement
*s
)
2183 s
->sa
->semantic2(sc
);
2186 void visit(SwitchStatement
*ss
)
2188 //printf("SwitchStatement::semantic(%p)\n", ss);
2192 result
= ss
; // already run
2195 bool conditionError
= false;
2196 ss
->condition
= semantic(ss
->condition
, sc
);
2197 ss
->condition
= resolveProperties(sc
, ss
->condition
);
2200 TypeEnum
*te
= NULL
;
2201 while (ss
->condition
->op
!= TOKerror
)
2203 // preserve enum type for final switches
2204 if (ss
->condition
->type
->ty
== Tenum
)
2205 te
= (TypeEnum
*)ss
->condition
->type
;
2206 if (ss
->condition
->type
->isString())
2208 // If it's not an array, cast it to one
2209 if (ss
->condition
->type
->ty
!= Tarray
)
2211 ss
->condition
= ss
->condition
->implicitCastTo(sc
, ss
->condition
->type
->nextOf()->arrayOf());
2213 ss
->condition
->type
= ss
->condition
->type
->constOf();
2216 ss
->condition
= integralPromotions(ss
->condition
, sc
);
2217 if (ss
->condition
->op
!= TOKerror
&& ss
->condition
->type
->isintegral())
2220 AggregateDeclaration
*ad
= isAggregate(ss
->condition
->type
);
2221 if (ad
&& ad
->aliasthis
&& ss
->condition
->type
!= att
)
2223 if (!att
&& ss
->condition
->type
->checkAliasThisRec())
2224 att
= ss
->condition
->type
;
2225 if (Expression
*e
= resolveAliasThis(sc
, ss
->condition
, true))
2232 if (ss
->condition
->op
!= TOKerror
)
2234 ss
->error("'%s' must be of integral or string type, it is a %s",
2235 ss
->condition
->toChars(), ss
->condition
->type
->toChars());
2236 conditionError
= true;
2240 if (checkNonAssignmentArrayOp(ss
->condition
))
2241 ss
->condition
= new ErrorExp();
2242 ss
->condition
= ss
->condition
->optimize(WANTvalue
);
2243 ss
->condition
= checkGC(sc
, ss
->condition
);
2244 if (ss
->condition
->op
== TOKerror
)
2245 conditionError
= true;
2247 bool needswitcherror
= false;
2249 ss
->lastVar
= sc
->lastVar
;
2255 ss
->cases
= new CaseStatements();
2256 sc
->noctor
++; // BUG: should use Scope::mergeCallSuper() for each case instead
2257 ss
->_body
= semantic(ss
->_body
, sc
);
2260 if (conditionError
|| (ss
->_body
&& ss
->_body
->isErrorStatement()))
2263 // Resolve any goto case's with exp
2264 for (size_t i
= 0; i
< ss
->gotoCases
.length
; i
++)
2266 GotoCaseStatement
*gcs
= ss
->gotoCases
[i
];
2270 gcs
->error("no case statement following goto case;");
2274 for (Scope
*scx
= sc
; scx
; scx
= scx
->enclosing
)
2278 for (size_t j
= 0; j
< scx
->sw
->cases
->length
; j
++)
2280 CaseStatement
*cs
= (*scx
->sw
->cases
)[j
];
2282 if (cs
->exp
->equals(gcs
->exp
))
2289 gcs
->error("case %s not found", gcs
->exp
->toChars());
2298 Type
*t
= ss
->condition
->type
;
2300 EnumDeclaration
*ed
= NULL
;
2301 if (t
&& ((ds
= t
->toDsymbol(sc
)) != NULL
))
2302 ed
= ds
->isEnumDeclaration(); // typedef'ed enum
2303 if (!ed
&& te
&& ((ds
= te
->toDsymbol(sc
)) != NULL
))
2304 ed
= ds
->isEnumDeclaration();
2307 size_t dim
= ed
->members
->length
;
2308 for (size_t i
= 0; i
< dim
; i
++)
2310 EnumMember
*em
= (*ed
->members
)[i
]->isEnumMember();
2313 for (size_t j
= 0; j
< ss
->cases
->length
; j
++)
2315 CaseStatement
*cs
= (*ss
->cases
)[j
];
2316 if (cs
->exp
->equals(em
->value()) ||
2317 (!cs
->exp
->type
->isString() && !em
->value()->type
->isString() &&
2318 cs
->exp
->toInteger() == em
->value()->toInteger()))
2321 ss
->error("enum member %s not represented in final switch", em
->toChars());
2329 needswitcherror
= true;
2332 if (!sc
->sw
->sdefault
&& (!ss
->isFinal
|| needswitcherror
|| global
.params
.useAssert
))
2334 ss
->hasNoDefault
= 1;
2336 if (!ss
->isFinal
&& (!ss
->_body
|| !ss
->_body
->isErrorStatement()))
2337 ss
->error("switch statement without a default; use 'final switch' or add 'default: assert(0);' or add 'default: break;'");
2339 // Generate runtime error if the default is hit
2340 Statements
*a
= new Statements();
2341 CompoundStatement
*cs
;
2344 if (global
.params
.useSwitchError
&&
2345 global
.params
.checkAction
!= CHECKACTION_halt
)
2347 if (global
.params
.checkAction
== CHECKACTION_C
)
2349 /* Rewrite as an assert(0) and let e2ir generate
2350 * the call to the C assert failure function
2352 s
= new ExpStatement(ss
->loc
, new AssertExp(ss
->loc
, new IntegerExp(ss
->loc
, 0, Type::tint32
)));
2355 s
= new SwitchErrorStatement(ss
->loc
);
2358 s
= new ExpStatement(ss
->loc
, new HaltExp(ss
->loc
));
2361 sc
->sw
->sdefault
= new DefaultStatement(ss
->loc
, s
);
2363 if (blockExit(ss
->_body
, sc
->func
, false) & BEfallthru
)
2364 a
->push(new BreakStatement(Loc(), NULL
));
2365 a
->push(sc
->sw
->sdefault
);
2366 cs
= new CompoundStatement(ss
->loc
, a
);
2370 if (ss
->checkLabel())
2379 result
= new ErrorStatement();
2382 void visit(CaseStatement
*cs
)
2384 SwitchStatement
*sw
= sc
->sw
;
2385 bool errors
= false;
2387 //printf("CaseStatement::semantic() %s\n", cs->toChars());
2388 sc
= sc
->startCTFE();
2389 cs
->exp
= semantic(cs
->exp
, sc
);
2390 cs
->exp
= resolveProperties(sc
, cs
->exp
);
2394 cs
->exp
= cs
->exp
->implicitCastTo(sc
, sw
->condition
->type
);
2395 cs
->exp
= cs
->exp
->optimize(WANTvalue
| WANTexpand
);
2397 Expression
*e
= cs
->exp
;
2398 // Remove all the casts the user and/or implicitCastTo may introduce
2399 // otherwise we'd sometimes fail the check below.
2400 while (e
->op
== TOKcast
)
2401 e
= ((CastExp
*)e
)->e1
;
2403 /* This is where variables are allowed as case expressions.
2405 if (e
->op
== TOKvar
)
2407 VarExp
*ve
= (VarExp
*)e
;
2408 VarDeclaration
*v
= ve
->var
->isVarDeclaration();
2409 Type
*t
= cs
->exp
->type
->toBasetype();
2410 if (v
&& (t
->isintegral() || t
->ty
== Tclass
))
2412 /* Flag that we need to do special code generation
2413 * for this, i.e. generate a sequence of if-then-else
2417 /* TODO check if v can be uninitialized at that point.
2419 if (!v
->isConst() && !v
->isImmutable())
2421 cs
->deprecation("case variables have to be const or immutable");
2426 cs
->error("case variables not allowed in final switch statements");
2430 /* Also check if the VarExp is declared in a scope outside of this one.
2431 * 'scx' is set to the scope of the switch statement.
2433 for (Scope
*scx
= sc
; scx
; scx
= scx
->enclosing
)
2435 if (scx
->enclosing
&& scx
->enclosing
->sw
== sw
)
2437 assert(scx
->sw
== sw
);
2439 if (!scx
->search(cs
->exp
->loc
, v
->ident
, NULL
))
2441 cs
->error("case variable `%s` declared at %s cannot be declared in switch body",
2442 v
->toChars(), v
->loc
.toChars());
2451 cs
->exp
= cs
->exp
->ctfeInterpret();
2453 if (StringExp
*se
= cs
->exp
->toStringExp())
2455 else if (cs
->exp
->op
!= TOKint64
&& cs
->exp
->op
!= TOKerror
)
2457 cs
->error("case must be a string or an integral constant, not %s", cs
->exp
->toChars());
2462 for (size_t i
= 0; i
< sw
->cases
->length
; i
++)
2464 CaseStatement
*cs2
= (*sw
->cases
)[i
];
2466 //printf("comparing '%s' with '%s'\n", cs->exp->toChars(), cs2->exp->toChars());
2467 if (cs2
->exp
->equals(cs
->exp
))
2469 cs
->error("duplicate case %s in switch statement", cs
->exp
->toChars());
2475 sw
->cases
->push(cs
);
2477 // Resolve any goto case's with no exp to this case statement
2478 for (size_t i
= 0; i
< sw
->gotoCases
.length
; )
2480 GotoCaseStatement
*gcs
= sw
->gotoCases
[i
];
2485 sw
->gotoCases
.remove(i
); // remove from array
2491 if (sc
->sw
->tf
!= sc
->tf
)
2493 cs
->error("switch and case are in different finally blocks");
2499 cs
->error("case not in switch statement");
2502 cs
->statement
= semantic(cs
->statement
, sc
);
2503 if (cs
->statement
->isErrorStatement())
2505 result
= cs
->statement
;
2508 if (errors
|| cs
->exp
->op
== TOKerror
)
2511 cs
->lastVar
= sc
->lastVar
;
2515 void visit(CaseRangeStatement
*crs
)
2517 SwitchStatement
*sw
= sc
->sw
;
2520 crs
->error("case range not in switch statement");
2524 //printf("CaseRangeStatement::semantic() %s\n", toChars());
2525 bool errors
= false;
2528 crs
->error("case ranges not allowed in final switch");
2532 sc
= sc
->startCTFE();
2533 crs
->first
= semantic(crs
->first
, sc
);
2534 crs
->first
= resolveProperties(sc
, crs
->first
);
2536 crs
->first
= crs
->first
->implicitCastTo(sc
, sw
->condition
->type
);
2537 crs
->first
= crs
->first
->ctfeInterpret();
2539 sc
= sc
->startCTFE();
2540 crs
->last
= semantic(crs
->last
, sc
);
2541 crs
->last
= resolveProperties(sc
, crs
->last
);
2543 crs
->last
= crs
->last
->implicitCastTo(sc
, sw
->condition
->type
);
2544 crs
->last
= crs
->last
->ctfeInterpret();
2546 if (crs
->first
->op
== TOKerror
|| crs
->last
->op
== TOKerror
|| errors
)
2549 semantic(crs
->statement
, sc
);
2553 uinteger_t fval
= crs
->first
->toInteger();
2554 uinteger_t lval
= crs
->last
->toInteger();
2557 if ( (crs
->first
->type
->isunsigned() && fval
> lval
) ||
2558 (!crs
->first
->type
->isunsigned() && (sinteger_t
)fval
> (sinteger_t
)lval
))
2560 crs
->error("first case %s is greater than last case %s",
2561 crs
->first
->toChars(), crs
->last
->toChars());
2566 if (lval
- fval
> 256)
2568 crs
->error("had %llu cases which is more than 256 cases in case range", lval
- fval
);
2576 /* This works by replacing the CaseRange with an array of Case's.
2578 * case a: .. case b: s;
2586 Statements
*statements
= new Statements();
2587 for (uinteger_t i
= fval
; i
!= lval
+ 1; i
++)
2589 Statement
*s
= crs
->statement
;
2590 if (i
!= lval
) // if not last case
2591 s
= new ExpStatement(crs
->loc
, (Expression
*)NULL
);
2592 Expression
*e
= new IntegerExp(crs
->loc
, i
, crs
->first
->type
);
2593 Statement
*cs
= new CaseStatement(crs
->loc
, e
, s
);
2594 statements
->push(cs
);
2596 Statement
*s
= new CompoundStatement(crs
->loc
, statements
);
2597 s
= semantic(s
, sc
);
2601 void visit(DefaultStatement
*ds
)
2603 //printf("DefaultStatement::semantic()\n");
2604 bool errors
= false;
2607 if (sc
->sw
->sdefault
)
2609 ds
->error("switch statement already has a default");
2612 sc
->sw
->sdefault
= ds
;
2614 if (sc
->sw
->tf
!= sc
->tf
)
2616 ds
->error("switch and default are in different finally blocks");
2619 if (sc
->sw
->isFinal
)
2621 ds
->error("default statement not allowed in final switch statement");
2627 ds
->error("default not in switch statement");
2630 ds
->statement
= semantic(ds
->statement
, sc
);
2631 if (errors
|| ds
->statement
->isErrorStatement())
2634 ds
->lastVar
= sc
->lastVar
;
2638 void visit(GotoDefaultStatement
*gds
)
2643 gds
->error("goto default not in switch statement");
2646 if (gds
->sw
->isFinal
)
2648 gds
->error("goto default not allowed in final switch statement");
2654 void visit(GotoCaseStatement
*gcs
)
2658 gcs
->error("goto case not in switch statement");
2664 gcs
->exp
= semantic(gcs
->exp
, sc
);
2665 gcs
->exp
= gcs
->exp
->implicitCastTo(sc
, sc
->sw
->condition
->type
);
2666 gcs
->exp
= gcs
->exp
->optimize(WANTvalue
);
2667 if (gcs
->exp
->op
== TOKerror
)
2671 sc
->sw
->gotoCases
.push(gcs
);
2675 void visit(ReturnStatement
*rs
)
2677 //printf("ReturnStatement::semantic() %s\n", toChars());
2679 FuncDeclaration
*fd
= sc
->parent
->isFuncDeclaration();
2682 fd
= fd
->fes
->func
; // fd is now function enclosing foreach
2684 TypeFunction
*tf
= (TypeFunction
*)fd
->type
;
2685 assert(tf
->ty
== Tfunction
);
2687 if (rs
->exp
&& rs
->exp
->op
== TOKvar
&& ((VarExp
*)rs
->exp
)->var
== fd
->vresult
)
2692 assert(rs
->caseDim
== 0);
2693 sc
->fes
->cases
->push(rs
);
2694 result
= new ReturnStatement(Loc(), new IntegerExp(sc
->fes
->cases
->length
+ 1));
2697 if (fd
->returnLabel
)
2699 GotoStatement
*gs
= new GotoStatement(rs
->loc
, Id::returnLabel
);
2700 gs
->label
= fd
->returnLabel
;
2706 fd
->returns
= new ReturnStatements();
2707 fd
->returns
->push(rs
);
2712 Type
*tret
= tf
->next
;
2713 Type
*tbret
= tret
? tret
->toBasetype() : NULL
;
2715 bool inferRef
= (tf
->isref
&& (fd
->storage_class
& STCauto
));
2716 Expression
*e0
= NULL
;
2718 bool errors
= false;
2719 if (sc
->flags
& SCOPEcontract
)
2721 rs
->error("return statements cannot be in contracts");
2724 if (sc
->os
&& sc
->os
->tok
!= TOKon_scope_failure
)
2726 rs
->error("return statements cannot be in %s bodies", Token::toChars(sc
->os
->tok
));
2731 rs
->error("return statements cannot be in finally bodies");
2735 if (fd
->isCtorDeclaration())
2739 rs
->error("cannot return expression from constructor");
2743 // Constructors implicitly do:
2745 rs
->exp
= new ThisExp(Loc());
2746 rs
->exp
->type
= tret
;
2750 fd
->hasReturnExp
|= (fd
->hasReturnExp
& 1 ? 16 : 1);
2752 FuncLiteralDeclaration
*fld
= fd
->isFuncLiteralDeclaration();
2754 rs
->exp
= inferType(rs
->exp
, tret
);
2755 else if (fld
&& fld
->treq
)
2756 rs
->exp
= inferType(rs
->exp
, fld
->treq
->nextOf()->nextOf());
2757 rs
->exp
= semantic(rs
->exp
, sc
);
2759 // for static alias this: https://issues.dlang.org/show_bug.cgi?id=17684
2760 if (rs
->exp
->op
== TOKtype
)
2761 rs
->exp
= resolveAliasThis(sc
, rs
->exp
);
2763 rs
->exp
= resolveProperties(sc
, rs
->exp
);
2764 if (rs
->exp
->checkType())
2765 rs
->exp
= new ErrorExp();
2766 if (FuncDeclaration
*f
= isFuncAddress(rs
->exp
))
2768 if (fd
->inferRetType
&& f
->checkForwardRef(rs
->exp
->loc
))
2769 rs
->exp
= new ErrorExp();
2771 if (checkNonAssignmentArrayOp(rs
->exp
))
2772 rs
->exp
= new ErrorExp();
2774 // Extract side-effect part
2775 rs
->exp
= Expression::extractLast(rs
->exp
, &e0
);
2776 if (rs
->exp
->op
== TOKcall
)
2777 rs
->exp
= valueNoDtor(rs
->exp
);
2780 e0
= e0
->optimize(WANTvalue
);
2782 /* Void-return function can have void typed expression
2783 * on return statement.
2785 if ((tbret
&& tbret
->ty
== Tvoid
) || rs
->exp
->type
->ty
== Tvoid
)
2787 if (rs
->exp
->type
->ty
!= Tvoid
)
2789 rs
->error("cannot return non-void from void function");
2792 rs
->exp
= new CastExp(rs
->loc
, rs
->exp
, Type::tvoid
);
2793 rs
->exp
= semantic(rs
->exp
, sc
);
2801 e0
= Expression::combine(e0
, rs
->exp
);
2805 e0
= checkGC(sc
, e0
);
2810 if (fd
->inferRetType
) // infer return type
2814 tf
->next
= rs
->exp
->type
;
2816 else if (tret
->ty
!= Terror
&& !rs
->exp
->type
->equals(tret
))
2818 int m1
= rs
->exp
->type
->implicitConvTo(tret
);
2819 int m2
= tret
->implicitConvTo(rs
->exp
->type
);
2820 //printf("exp->type = %s m2<-->m1 tret %s\n", rs->exp->type->toChars(), tret->toChars());
2821 //printf("m1 = %d, m2 = %d\n", m1, m2);
2826 tf
->next
= rs
->exp
->type
;
2829 else if (rs
->exp
->op
!= TOKerror
)
2831 rs
->error("mismatched function return type inference of %s and %s",
2832 rs
->exp
->type
->toChars(), tret
->toChars());
2834 tf
->next
= Type::terror
;
2839 tbret
= tret
->toBasetype();
2842 if (inferRef
) // deduce 'auto ref'
2844 /* Determine "refness" of function return:
2845 * if it's an lvalue, return by ref, else return by value
2847 if (rs
->exp
->isLvalue())
2849 /* May return by ref
2851 if (checkReturnEscapeRef(sc
, rs
->exp
, true))
2852 tf
->isref
= false; // return by value
2855 tf
->isref
= false; // return by value
2857 /* The "refness" is determined by all of return statements.
2859 * return 3; return x; // ok, x can be a value
2860 * return x; return 3; // ok, x can be a value
2865 if (fd
->nrvo_can
&& rs
->exp
->op
== TOKvar
)
2867 VarExp
*ve
= (VarExp
*)rs
->exp
;
2868 VarDeclaration
*v
= ve
->var
->isVarDeclaration();
2872 // Function returns a reference
2876 else if (!v
|| v
->isOut() || v
->isRef())
2878 else if (fd
->nrvo_var
== NULL
)
2880 if (!v
->isDataseg() && !v
->isParameter() && v
->toParent2() == fd
)
2882 //printf("Setting nrvo to %s\n", v->toChars());
2888 else if (fd
->nrvo_var
!= v
)
2891 else //if (!exp->isLvalue()) // keep NRVO-ability
2899 // infer return type
2900 if (fd
->inferRetType
)
2902 if (tf
->next
&& tf
->next
->ty
!= Tvoid
)
2904 if (tf
->next
->ty
!= Terror
)
2906 rs
->error("mismatched function return type inference of void and %s",
2907 tf
->next
->toChars());
2910 tf
->next
= Type::terror
;
2913 tf
->next
= Type::tvoid
;
2916 tbret
= tret
->toBasetype();
2919 if (inferRef
) // deduce 'auto ref'
2922 if (tbret
->ty
!= Tvoid
) // if non-void return
2924 if (tbret
->ty
!= Terror
)
2925 rs
->error("return expression expected");
2928 else if (fd
->isMain())
2930 // main() returns 0, even if it returns void
2931 rs
->exp
= new IntegerExp(0);
2935 // If any branches have called a ctor, but this branch hasn't, it's an error
2936 if (sc
->callSuper
& CSXany_ctor
&&
2937 !(sc
->callSuper
& (CSXthis_ctor
| CSXsuper_ctor
)))
2939 rs
->error("return without calling constructor");
2942 sc
->callSuper
|= CSXreturn
;
2945 AggregateDeclaration
*ad
= fd
->isMember2();
2947 size_t dim
= sc
->fieldinit_dim
;
2948 for (size_t i
= 0; i
< dim
; i
++)
2950 VarDeclaration
*v
= ad
->fields
[i
];
2951 bool mustInit
= (v
->storage_class
& STCnodefaultctor
||
2952 v
->type
->needsNested());
2953 if (mustInit
&& !(sc
->fieldinit
[i
] & CSXthis_ctor
))
2955 rs
->error("an earlier return statement skips field %s initialization", v
->toChars());
2958 sc
->fieldinit
[i
] |= CSXreturn
;
2969 // Send out "case receiver" statement to the foreach.
2971 Statement
*s
= new ReturnStatement(Loc(), rs
->exp
);
2972 sc
->fes
->cases
->push(s
);
2974 // Immediately rewrite "this" return statement as:
2975 // return cases->length+1;
2976 rs
->exp
= new IntegerExp(sc
->fes
->cases
->length
+ 1);
2979 result
= new CompoundStatement(rs
->loc
, new ExpStatement(rs
->loc
, e0
), rs
);
2987 fd
->buildResultVar(NULL
, rs
->exp
->type
);
2988 bool r
= fd
->vresult
->checkNestedReference(sc
, Loc());
2989 assert(!r
); // vresult should be always accessible
2991 // Send out "case receiver" statement to the foreach.
2993 Statement
*s
= new ReturnStatement(Loc(), new VarExp(Loc(), fd
->vresult
));
2994 sc
->fes
->cases
->push(s
);
2996 // Save receiver index for the later rewriting from:
2999 // vresult = exp; retrun caseDim;
3000 rs
->caseDim
= sc
->fes
->cases
->length
+ 1;
3006 fd
->returns
= new ReturnStatements();
3007 fd
->returns
->push(rs
);
3011 result
= new CompoundStatement(rs
->loc
, new ExpStatement(rs
->loc
, e0
), rs
);
3017 void visit(BreakStatement
*bs
)
3019 //printf("BreakStatement::semantic()\n");
3021 // break Identifier;
3024 bs
->ident
= fixupLabelName(sc
, bs
->ident
);
3026 FuncDeclaration
*thisfunc
= sc
->func
;
3028 for (Scope
*scx
= sc
; scx
; scx
= scx
->enclosing
)
3030 if (scx
->func
!= thisfunc
) // if in enclosing function
3032 if (sc
->fes
) // if this is the body of a foreach
3034 /* Post this statement to the fes, and replace
3035 * it with a return value that caller will put into
3036 * a switch. Caller will figure out where the break
3037 * label actually is.
3038 * Case numbers start with 2, not 0, as 0 is continue
3041 sc
->fes
->cases
->push(bs
);
3042 result
= new ReturnStatement(Loc(), new IntegerExp(sc
->fes
->cases
->length
+ 1));
3045 break; // can't break to it
3048 LabelStatement
*ls
= scx
->slabel
;
3049 if (ls
&& ls
->ident
== bs
->ident
)
3051 Statement
*s
= ls
->statement
;
3053 if (!s
|| !s
->hasBreak())
3054 bs
->error("label '%s' has no break", bs
->ident
->toChars());
3055 else if (ls
->tf
!= sc
->tf
)
3056 bs
->error("cannot break out of finally block");
3066 bs
->error("enclosing label '%s' for break not found", bs
->ident
->toChars());
3069 else if (!sc
->sbreak
)
3071 if (sc
->os
&& sc
->os
->tok
!= TOKon_scope_failure
)
3073 bs
->error("break is not inside %s bodies", Token::toChars(sc
->os
->tok
));
3077 // Replace break; with return 1;
3078 result
= new ReturnStatement(Loc(), new IntegerExp(1));
3082 bs
->error("break is not inside a loop or switch");
3085 else if (sc
->sbreak
->isForwardingStatement())
3087 bs
->error("must use labeled `break` within `static foreach`");
3092 void visit(ContinueStatement
*cs
)
3094 //printf("ContinueStatement::semantic() %p\n", cs);
3097 cs
->ident
= fixupLabelName(sc
, cs
->ident
);
3100 FuncDeclaration
*thisfunc
= sc
->func
;
3102 for (scx
= sc
; scx
; scx
= scx
->enclosing
)
3106 if (scx
->func
!= thisfunc
) // if in enclosing function
3108 if (sc
->fes
) // if this is the body of a foreach
3110 for (; scx
; scx
= scx
->enclosing
)
3113 if (ls
&& ls
->ident
== cs
->ident
&& ls
->statement
== sc
->fes
)
3115 // Replace continue ident; with return 0;
3116 result
= new ReturnStatement(Loc(), new IntegerExp(0));
3121 /* Post this statement to the fes, and replace
3122 * it with a return value that caller will put into
3123 * a switch. Caller will figure out where the break
3124 * label actually is.
3125 * Case numbers start with 2, not 0, as 0 is continue
3128 sc
->fes
->cases
->push(cs
);
3129 result
= new ReturnStatement(Loc(), new IntegerExp(sc
->fes
->cases
->length
+ 1));
3132 break; // can't continue to it
3136 if (ls
&& ls
->ident
== cs
->ident
)
3138 Statement
*s
= ls
->statement
;
3140 if (!s
|| !s
->hasContinue())
3141 cs
->error("label '%s' has no continue", cs
->ident
->toChars());
3142 else if (ls
->tf
!= sc
->tf
)
3143 cs
->error("cannot continue out of finally block");
3152 cs
->error("enclosing label '%s' for continue not found", cs
->ident
->toChars());
3155 else if (!sc
->scontinue
)
3157 if (sc
->os
&& sc
->os
->tok
!= TOKon_scope_failure
)
3159 cs
->error("continue is not inside %s bodies", Token::toChars(sc
->os
->tok
));
3163 // Replace continue; with return 0;
3164 result
= new ReturnStatement(Loc(), new IntegerExp(0));
3168 cs
->error("continue is not inside a loop");
3171 else if (sc
->scontinue
->isForwardingStatement())
3173 cs
->error("must use labeled `continue` within `static foreach`");
3178 void visit(SynchronizedStatement
*ss
)
3182 ss
->exp
= semantic(ss
->exp
, sc
);
3183 ss
->exp
= resolveProperties(sc
, ss
->exp
);
3184 ss
->exp
= ss
->exp
->optimize(WANTvalue
);
3185 ss
->exp
= checkGC(sc
, ss
->exp
);
3186 if (ss
->exp
->op
== TOKerror
)
3188 ClassDeclaration
*cd
= ss
->exp
->type
->isClassHandle();
3191 ss
->error("can only synchronize on class objects, not '%s'", ss
->exp
->type
->toChars());
3194 else if (cd
->isInterfaceDeclaration())
3196 /* Cast the interface to an object, as the object has the monitor,
3197 * not the interface.
3199 if (!ClassDeclaration::object
)
3201 ss
->error("missing or corrupt object.d");
3205 Type
*t
= ClassDeclaration::object
->type
;
3206 t
= t
->semantic(Loc(), sc
)->toBasetype();
3207 assert(t
->ty
== Tclass
);
3209 ss
->exp
= new CastExp(ss
->loc
, ss
->exp
, t
);
3210 ss
->exp
= semantic(ss
->exp
, sc
);
3215 * _d_monitorenter(tmp);
3216 * try { body } finally { _d_monitorexit(tmp); }
3218 VarDeclaration
*tmp
= copyToTemp(0, "__sync", ss
->exp
);
3221 Statements
*cs
= new Statements();
3222 cs
->push(new ExpStatement(ss
->loc
, tmp
));
3224 Parameters
* args
= new Parameters
;
3225 args
->push(new Parameter(0, ClassDeclaration::object
->type
, NULL
, NULL
));
3227 FuncDeclaration
*fdenter
= FuncDeclaration::genCfunc(args
, Type::tvoid
, Id::monitorenter
);
3228 Expression
*e
= new CallExp(ss
->loc
, new VarExp(ss
->loc
, fdenter
, false), new VarExp(ss
->loc
, tmp
));
3229 e
->type
= Type::tvoid
; // do not run semantic on e
3230 cs
->push(new ExpStatement(ss
->loc
, e
));
3232 FuncDeclaration
*fdexit
= FuncDeclaration::genCfunc(args
, Type::tvoid
, Id::monitorexit
);
3233 e
= new CallExp(ss
->loc
, new VarExp(ss
->loc
, fdexit
, false), new VarExp(ss
->loc
, tmp
));
3234 e
->type
= Type::tvoid
; // do not run semantic on e
3235 Statement
*s
= new ExpStatement(ss
->loc
, e
);
3236 s
= new TryFinallyStatement(ss
->loc
, ss
->_body
, s
);
3239 s
= new CompoundStatement(ss
->loc
, cs
);
3240 result
= semantic(s
, sc
);
3245 /* Generate our own critical section, then rewrite as:
3246 * __gshared byte[CriticalSection.sizeof] critsec;
3247 * _d_criticalenter(critsec.ptr);
3248 * try { body } finally { _d_criticalexit(critsec.ptr); }
3250 Identifier
*id
= Identifier::generateId("__critsec");
3251 Type
*t
= Type::tint8
->sarrayOf(Target::ptrsize
+ Target::critsecsize());
3252 VarDeclaration
*tmp
= new VarDeclaration(ss
->loc
, t
, id
, NULL
);
3253 tmp
->storage_class
|= STCtemp
| STCgshared
| STCstatic
;
3255 Statements
*cs
= new Statements();
3256 cs
->push(new ExpStatement(ss
->loc
, tmp
));
3258 /* This is just a dummy variable for "goto skips declaration" error.
3259 * Backend optimizer could remove this unused variable.
3261 VarDeclaration
*v
= new VarDeclaration(ss
->loc
, Type::tvoidptr
, Identifier::generateId("__sync"), NULL
);
3263 cs
->push(new ExpStatement(ss
->loc
, v
));
3265 Parameters
* args
= new Parameters
;
3266 args
->push(new Parameter(0, t
->pointerTo(), NULL
, NULL
));
3268 FuncDeclaration
*fdenter
= FuncDeclaration::genCfunc(args
, Type::tvoid
, Id::criticalenter
, STCnothrow
);
3269 Expression
*e
= new DotIdExp(ss
->loc
, new VarExp(ss
->loc
, tmp
), Id::ptr
);
3270 e
= semantic(e
, sc
);
3271 e
= new CallExp(ss
->loc
, new VarExp(ss
->loc
, fdenter
, false), e
);
3272 e
->type
= Type::tvoid
; // do not run semantic on e
3273 cs
->push(new ExpStatement(ss
->loc
, e
));
3275 FuncDeclaration
*fdexit
= FuncDeclaration::genCfunc(args
, Type::tvoid
, Id::criticalexit
, STCnothrow
);
3276 e
= new DotIdExp(ss
->loc
, new VarExp(ss
->loc
, tmp
), Id::ptr
);
3277 e
= semantic(e
, sc
);
3278 e
= new CallExp(ss
->loc
, new VarExp(ss
->loc
, fdexit
, false), e
);
3279 e
->type
= Type::tvoid
; // do not run semantic on e
3280 Statement
*s
= new ExpStatement(ss
->loc
, e
);
3281 s
= new TryFinallyStatement(ss
->loc
, ss
->_body
, s
);
3284 s
= new CompoundStatement(ss
->loc
, cs
);
3285 result
= semantic(s
, sc
);
3290 ss
->_body
= semantic(ss
->_body
, sc
);
3291 if (ss
->_body
&& ss
->_body
->isErrorStatement())
3299 void visit(WithStatement
*ws
)
3304 //printf("WithStatement::semantic()\n");
3305 ws
->exp
= semantic(ws
->exp
, sc
);
3306 ws
->exp
= resolveProperties(sc
, ws
->exp
);
3307 ws
->exp
= ws
->exp
->optimize(WANTvalue
);
3308 ws
->exp
= checkGC(sc
, ws
->exp
);
3309 if (ws
->exp
->op
== TOKerror
)
3311 if (ws
->exp
->op
== TOKscope
)
3313 sym
= new WithScopeSymbol(ws
);
3314 sym
->parent
= sc
->scopesym
;
3315 sym
->endlinnum
= ws
->endloc
.linnum
;
3317 else if (ws
->exp
->op
== TOKtype
)
3319 Dsymbol
*s
= ((TypeExp
*)ws
->exp
)->type
->toDsymbol(sc
);
3320 if (!s
|| !s
->isScopeDsymbol())
3322 ws
->error("with type %s has no members", ws
->exp
->toChars());
3325 sym
= new WithScopeSymbol(ws
);
3326 sym
->parent
= sc
->scopesym
;
3327 sym
->endlinnum
= ws
->endloc
.linnum
;
3331 Type
*t
= ws
->exp
->type
->toBasetype();
3333 Expression
*olde
= ws
->exp
;
3334 if (t
->ty
== Tpointer
)
3336 ws
->exp
= new PtrExp(ws
->loc
, ws
->exp
);
3337 ws
->exp
= semantic(ws
->exp
, sc
);
3338 t
= ws
->exp
->type
->toBasetype();
3342 t
= t
->toBasetype();
3343 if (t
->isClassHandle())
3345 init
= new ExpInitializer(ws
->loc
, ws
->exp
);
3346 ws
->wthis
= new VarDeclaration(ws
->loc
, ws
->exp
->type
, Id::withSym
, init
);
3347 ws
->wthis
->semantic(sc
);
3349 sym
= new WithScopeSymbol(ws
);
3350 sym
->parent
= sc
->scopesym
;
3351 sym
->endlinnum
= ws
->endloc
.linnum
;
3353 else if (t
->ty
== Tstruct
)
3355 if (!ws
->exp
->isLvalue())
3359 * auto __withtmp = exp
3366 VarDeclaration
*tmp
= copyToTemp(0, "__withtmp", ws
->exp
);
3368 ExpStatement
*es
= new ExpStatement(ws
->loc
, tmp
);
3369 ws
->exp
= new VarExp(ws
->loc
, tmp
);
3370 Statement
*ss
= new ScopeStatement(ws
->loc
, new CompoundStatement(ws
->loc
, es
, ws
), ws
->endloc
);
3371 result
= semantic(ss
, sc
);
3374 Expression
*e
= ws
->exp
->addressOf();
3375 init
= new ExpInitializer(ws
->loc
, e
);
3376 ws
->wthis
= new VarDeclaration(ws
->loc
, e
->type
, Id::withSym
, init
);
3377 ws
->wthis
->semantic(sc
);
3378 sym
= new WithScopeSymbol(ws
);
3379 // Need to set the scope to make use of resolveAliasThis
3381 sym
->parent
= sc
->scopesym
;
3382 sym
->endlinnum
= ws
->endloc
.linnum
;
3386 ws
->error("with expressions must be aggregate types or pointers to them, not '%s'", olde
->type
->toChars());
3396 ws
->_body
= semantic(ws
->_body
, sc
);
3398 if (ws
->_body
&& ws
->_body
->isErrorStatement())
3408 void visit(TryCatchStatement
*tcs
)
3410 if (!global
.params
.useExceptions
)
3412 tcs
->error("Cannot use try-catch statements with -betterC");
3416 if (!ClassDeclaration::throwable
)
3418 tcs
->error("Cannot use try-catch statements because `object.Throwable` was not declared");
3423 const unsigned FLAGcpp
= 1;
3424 const unsigned FLAGd
= 2;
3426 tcs
->_body
= semanticScope(tcs
->_body
, sc
, NULL
, NULL
);
3429 /* Even if body is empty, still do semantic analysis on catches
3431 bool catchErrors
= false;
3432 for (size_t i
= 0; i
< tcs
->catches
->length
; i
++)
3434 Catch
*c
= (*tcs
->catches
)[i
];
3441 ClassDeclaration
*cd
= c
->type
->toBasetype()->isClassHandle();
3442 flags
|= cd
->isCPPclass() ? FLAGcpp
: FLAGd
;
3444 // Determine if current catch 'hides' any previous catches
3445 for (size_t j
= 0; j
< i
; j
++)
3447 Catch
*cj
= (*tcs
->catches
)[j
];
3448 const char *si
= c
->loc
.toChars();
3449 const char *sj
= cj
->loc
.toChars();
3451 if (c
->type
->toBasetype()->implicitConvTo(cj
->type
->toBasetype()))
3453 tcs
->error("catch at %s hides catch at %s", sj
, si
);
3461 if (flags
== (FLAGcpp
| FLAGd
))
3463 tcs
->error("cannot mix catching D and C++ exceptions in the same try-catch");
3471 if (tcs
->_body
->isErrorStatement())
3473 result
= tcs
->_body
;
3477 /* If the try body never throws, we can eliminate any catches
3478 * of recoverable exceptions.
3481 if (!(blockExit(tcs
->_body
, sc
->func
, false) & BEthrow
) && ClassDeclaration::exception
)
3483 for (size_t i
= 0; i
< tcs
->catches
->length
; i
++)
3485 Catch
*c
= (*tcs
->catches
)[i
];
3487 /* If catch exception type is derived from Exception
3489 if (c
->type
->toBasetype()->implicitConvTo(ClassDeclaration::exception
->type
) &&
3490 (!c
->handler
|| !c
->handler
->comeFrom()))
3492 // Remove c from the array of catches
3493 tcs
->catches
->remove(i
);
3499 if (tcs
->catches
->length
== 0)
3501 result
= tcs
->_body
->hasCode() ? tcs
->_body
: NULL
;
3508 void visit(TryFinallyStatement
*tfs
)
3510 //printf("TryFinallyStatement::semantic()\n");
3511 tfs
->_body
= semantic(tfs
->_body
, sc
);
3515 sc
->scontinue
= NULL
; // no break or continue out of finally block
3516 tfs
->finalbody
= semanticNoScope(tfs
->finalbody
, sc
);
3521 result
= tfs
->finalbody
;
3525 if (!tfs
->finalbody
)
3527 result
= tfs
->_body
;
3531 int blockexit
= blockExit(tfs
->_body
, sc
->func
, false);
3533 // if not worrying about exceptions
3534 if (!(global
.params
.useExceptions
&& ClassDeclaration::throwable
))
3535 blockexit
&= ~BEthrow
; // don't worry about paths that otherwise may throw
3537 // Don't care about paths that halt, either
3538 if ((blockexit
& ~BEhalt
) == BEfallthru
)
3540 result
= new CompoundStatement(tfs
->loc
, tfs
->_body
, tfs
->finalbody
);
3546 void visit(OnScopeStatement
*oss
)
3548 if (oss
->tok
!= TOKon_scope_exit
)
3550 // scope(success) and scope(failure) are rewritten to try-catch(-finally) statement,
3551 // so the generated catch block cannot be placed in finally block.
3552 // See also Catch::semantic.
3553 if (sc
->os
&& sc
->os
->tok
!= TOKon_scope_failure
)
3555 // If enclosing is scope(success) or scope(exit), this will be placed in finally block.
3556 oss
->error("cannot put %s statement inside %s", Token::toChars(oss
->tok
), Token::toChars(sc
->os
->tok
));
3561 oss
->error("cannot put %s statement inside finally block", Token::toChars(oss
->tok
));
3569 if (oss
->tok
!= TOKon_scope_failure
)
3571 // Jump out from scope(failure) block is allowed.
3573 sc
->scontinue
= NULL
;
3575 oss
->statement
= semanticNoScope(oss
->statement
, sc
);
3578 if (!oss
->statement
|| oss
->statement
->isErrorStatement())
3580 result
= oss
->statement
;
3586 void visit(ThrowStatement
*ts
)
3588 //printf("ThrowStatement::semantic()\n");
3590 if (!global
.params
.useExceptions
)
3592 ts
->error("Cannot use `throw` statements with -betterC");
3596 if (!ClassDeclaration::throwable
)
3598 ts
->error("Cannot use `throw` statements because `object.Throwable` was not declared");
3602 FuncDeclaration
*fd
= sc
->parent
->isFuncDeclaration();
3603 fd
->hasReturnExp
|= 2;
3605 ts
->exp
= semantic(ts
->exp
, sc
);
3606 ts
->exp
= resolveProperties(sc
, ts
->exp
);
3607 ts
->exp
= checkGC(sc
, ts
->exp
);
3608 if (ts
->exp
->op
== TOKerror
)
3611 checkThrowEscape(sc
, ts
->exp
, false);
3613 ClassDeclaration
*cd
= ts
->exp
->type
->toBasetype()->isClassHandle();
3614 if (!cd
|| ((cd
!= ClassDeclaration::throwable
) && !ClassDeclaration::throwable
->isBaseOf(cd
, NULL
)))
3616 ts
->error("can only throw class objects derived from Throwable, not type %s", ts
->exp
->type
->toChars());
3623 void visit(DebugStatement
*ds
)
3628 sc
->flags
|= SCOPEdebug
;
3629 ds
->statement
= semantic(ds
->statement
, sc
);
3632 result
= ds
->statement
;
3635 void visit(GotoStatement
*gs
)
3637 //printf("GotoStatement::semantic()\n");
3638 FuncDeclaration
*fd
= sc
->func
;
3640 gs
->ident
= fixupLabelName(sc
, gs
->ident
);
3641 gs
->label
= fd
->searchLabel(gs
->ident
);
3644 gs
->lastVar
= sc
->lastVar
;
3646 if (!gs
->label
->statement
&& sc
->fes
)
3648 /* Either the goto label is forward referenced or it
3649 * is in the function that the enclosing foreach is in.
3650 * Can't know yet, so wrap the goto in a scope statement
3651 * so we can patch it later, and add it to a 'look at this later'
3654 ScopeStatement
*ss
= new ScopeStatement(gs
->loc
, gs
, gs
->loc
);
3655 sc
->fes
->gotos
->push(ss
); // 'look at this later' list
3660 // Add to fwdref list to check later
3661 if (!gs
->label
->statement
)
3664 fd
->gotos
= new GotoStatements();
3665 fd
->gotos
->push(gs
);
3667 else if (gs
->checkLabel())
3673 void visit(LabelStatement
*ls
)
3675 //printf("LabelStatement::semantic()\n");
3676 FuncDeclaration
*fd
= sc
->parent
->isFuncDeclaration();
3678 ls
->ident
= fixupLabelName(sc
, ls
->ident
);
3681 ls
->lastVar
= sc
->lastVar
;
3683 LabelDsymbol
*ls2
= fd
->searchLabel(ls
->ident
);
3686 ls
->error("label '%s' already defined", ls2
->toChars());
3690 ls2
->statement
= ls
;
3693 sc
->scopesym
= sc
->enclosing
->scopesym
;
3694 sc
->callSuper
|= CSXlabel
;
3697 size_t dim
= sc
->fieldinit_dim
;
3698 for (size_t i
= 0; i
< dim
; i
++)
3699 sc
->fieldinit
[i
] |= CSXlabel
;
3703 ls
->statement
= semantic(ls
->statement
, sc
);
3709 void visit(AsmStatement
*s
)
3711 result
= asmSemantic(s
, sc
);
3714 void visit(CompoundAsmStatement
*cas
)
3716 // Apply postfix attributes of the asm block to each statement.
3718 sc
->stc
|= cas
->stc
;
3720 for (size_t i
= 0; i
< cas
->statements
->length
; i
++)
3722 Statement
*s
= (*cas
->statements
)[i
];
3723 (*cas
->statements
)[i
] = s
? semantic(s
, sc
) : NULL
;
3727 // use setImpure/setGC when the deprecation cycle is over
3729 if (!(cas
->stc
& STCpure
) && (purity
= sc
->func
->isPureBypassingInference()) != PUREimpure
&& purity
!= PUREfwdref
)
3730 cas
->deprecation("asm statement is assumed to be impure - mark it with 'pure' if it is not");
3731 if (!(cas
->stc
& STCnogc
) && sc
->func
->isNogcBypassingInference())
3732 cas
->deprecation("asm statement is assumed to use the GC - mark it with '@nogc' if it does not");
3733 if (!(cas
->stc
& (STCtrusted
|STCsafe
)) && sc
->func
->setUnsafe())
3734 cas
->error("asm statement is assumed to be @system - mark it with '@trusted' if it is not");
3740 void visit(ImportStatement
*imps
)
3742 for (size_t i
= 0; i
< imps
->imports
->length
; i
++)
3744 Import
*s
= (*imps
->imports
)[i
]->isImport();
3745 assert(!s
->aliasdecls
.length
);
3746 for (size_t j
= 0; j
< s
->names
.length
; j
++)
3748 Identifier
*name
= s
->names
[j
];
3749 Identifier
*alias
= s
->aliases
[j
];
3754 TypeIdentifier
*tname
= new TypeIdentifier(s
->loc
, name
);
3755 AliasDeclaration
*ad
= new AliasDeclaration(s
->loc
, alias
, tname
);
3757 s
->aliasdecls
.push(ad
);
3761 // https://issues.dlang.org/show_bug.cgi?id=19942
3762 // If the module that's being imported doesn't exist, don't add it to the symbol table
3763 // for the current scope.
3766 Module::addDeferredSemantic2(s
); // Bugzilla 14666
3769 for (size_t j
= 0; j
< s
->aliasdecls
.length
; j
++)
3771 sc
->insert(s
->aliasdecls
[j
]);
3779 Statement
*semantic(Statement
*s
, Scope
*sc
)
3781 StatementSemanticVisitor v
= StatementSemanticVisitor(sc
);
3786 void semantic(Catch
*c
, Scope
*sc
)
3788 //printf("Catch::semantic(%s)\n", ident->toChars());
3790 if (sc
->os
&& sc
->os
->tok
!= TOKon_scope_failure
)
3792 // If enclosing is scope(success) or scope(exit), this will be placed in finally block.
3793 error(c
->loc
, "cannot put catch statement inside %s", Token::toChars(sc
->os
->tok
));
3798 /* This is because the _d_local_unwind() gets the stack munged
3799 * up on this. The workaround is to place any try-catches into
3800 * a separate function, and call that.
3801 * To fix, have the compiler automatically convert the finally
3802 * body into a nested function.
3804 error(c
->loc
, "cannot put catch statement inside finally block");
3808 ScopeDsymbol
*sym
= new ScopeDsymbol();
3809 sym
->parent
= sc
->scopesym
;
3814 deprecation(c
->loc
, "catch statement without an exception specification is deprecated; use catch(Throwable) for old behavior");
3816 // reference .object.Throwable
3817 c
->type
= getThrowable();
3819 c
->type
= c
->type
->semantic(c
->loc
, sc
);
3820 if (c
->type
== Type::terror
)
3824 ClassDeclaration
*cd
= c
->type
->toBasetype()->isClassHandle();
3827 error(c
->loc
, "can only catch class objects, not '%s'", c
->type
->toChars());
3830 else if (cd
->isCPPclass())
3832 if (!Target::cppExceptions
)
3834 error(c
->loc
, "catching C++ class objects not supported for this target");
3837 if (sc
->func
&& !sc
->intypeof
&& !c
->internalCatch
&& sc
->func
->setUnsafe())
3839 error(c
->loc
, "cannot catch C++ class objects in @safe code");
3843 else if (cd
!= ClassDeclaration::throwable
&& !ClassDeclaration::throwable
->isBaseOf(cd
, NULL
))
3845 error(c
->loc
, "can only catch class objects derived from Throwable, not '%s'", c
->type
->toChars());
3848 else if (sc
->func
&& !sc
->intypeof
&& !c
->internalCatch
&&
3849 cd
!= ClassDeclaration::exception
&& !ClassDeclaration::exception
->isBaseOf(cd
, NULL
) &&
3850 sc
->func
->setUnsafe())
3852 error(c
->loc
, "can only catch class objects derived from Exception in @safe code, not '%s'", c
->type
->toChars());
3858 c
->var
= new VarDeclaration(c
->loc
, c
->type
, c
->ident
, NULL
);
3859 c
->var
->semantic(sc
);
3862 c
->handler
= semantic(c
->handler
, sc
);
3863 if (c
->handler
&& c
->handler
->isErrorStatement())
3869 Statement
*semanticNoScope(Statement
*s
, Scope
*sc
)
3871 //printf("Statement::semanticNoScope() %s\n", toChars());
3872 if (!s
->isCompoundStatement() && !s
->isScopeStatement())
3874 s
= new CompoundStatement(s
->loc
, s
); // so scopeCode() gets called
3876 s
= semantic(s
, sc
);
3880 // Same as semanticNoScope(), but do create a new scope
3881 Statement
*semanticScope(Statement
*s
, Scope
*sc
, Statement
*sbreak
, Statement
*scontinue
)
3883 ScopeDsymbol
*sym
= new ScopeDsymbol();
3884 sym
->parent
= sc
->scopesym
;
3885 Scope
*scd
= sc
->push(sym
);
3887 scd
->sbreak
= sbreak
;
3889 scd
->scontinue
= scontinue
;
3890 s
= semanticNoScope(s
, scd
);
3895 /*******************
3896 * See StatementSemanticVisitor.makeTupleForeach. This is a simple
3897 * wrapper that returns the generated statements/declarations.
3899 Statement
*makeTupleForeachStatic(Scope
*sc
, ForeachStatement
*fs
, bool needExpansion
)
3901 StatementSemanticVisitor v
= StatementSemanticVisitor(sc
);
3902 v
.makeTupleForeachStatic(fs
, needExpansion
);
3906 Dsymbols
*makeTupleForeachStaticDecl(Scope
*sc
, ForeachStatement
*fs
, Dsymbols
*dbody
, bool needExpansion
)
3908 StatementSemanticVisitor v
= StatementSemanticVisitor(sc
);
3909 return v
.makeTupleForeachStaticDecl(fs
, dbody
, needExpansion
);