Debian Codegraph @

gcc/d/dmd/blockexit.d

raw
1 /**
2 * Find out in what ways control flow can exit a statement block.
3 *
4 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/blockexit.d, _blockexit.d)
8 * Documentation: https://dlang.org/phobos/dmd_blockexit.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/blockexit.d
10 */
11
12 module dmd.blockexit;
13
14 import core.stdc.stdio;
15
16 import dmd.arraytypes;
17 import dmd.astenums;
18 import dmd.canthrow;
19 import dmd.dclass;
20 import dmd.declaration;
21 import dmd.errorsink;
22 import dmd.expression;
23 import dmd.func;
24 import dmd.globals;
25 import dmd.id;
26 import dmd.identifier;
27 import dmd.location;
28 import dmd.mtype;
29 import dmd.statement;
30 import dmd.tokens;
31
32 /**
33 * BE stands for BlockExit.
34 *
35 * It indicates if a statement does transfer control to another block.
36 * A block is a sequence of statements enclosed in { }
37 */
38 enum BE : int
39 {
40 none = 0,
41 fallthru = 1,
42 throw_ = 2,
43 return_ = 4,
44 goto_ = 8,
45 halt = 0x10,
46 break_ = 0x20,
47 continue_ = 0x40,
48 errthrow = 0x80,
49 any = (fallthru | throw_ | return_ | goto_ | halt),
50 }
51
52
53 /*********************************************
54 * Determine mask of ways that a statement can exit.
55 *
56 * Only valid after semantic analysis.
57 * Params:
58 * s = statement to check for block exit status
59 * func = function that statement s is in
60 * eSink = generate an error if it throws
61 * Returns:
62 * BE.xxxx
63 */
64 int blockExit(Statement s, FuncDeclaration func, ErrorSink eSink)
65 {
66 int result = BE.none;
67
68 void visitDefaultCase(Statement s)
69 {
70 printf("Statement::blockExit(%p)\n", s);
71 printf("%s\n", s.toChars());
72 assert(0);
73 }
74
75 void visitError(ErrorStatement s)
76 {
77 result = BE.none;
78 }
79
80 void visitExp(ExpStatement s)
81 {
82 result = BE.fallthru;
83 if (!s.exp)
84 return;
85
86 if (s.exp.op == EXP.halt)
87 {
88 result = BE.halt;
89 return;
90 }
91 if (AssertExp a = s.exp.isAssertExp())
92 {
93 if (a.e1.toBool().hasValue(false)) // if it's an assert(0)
94 {
95 result = BE.halt;
96 return;
97 }
98 }
99 if (s.exp.type && s.exp.type.toBasetype().isTypeNoreturn())
100 result = BE.halt;
101 result |= canThrow(s.exp, func, eSink);
102 }
103
104 void visitDtorExp(DtorExpStatement s)
105 {
106 visitExp(s);
107 }
108
109 void visitMixin(MixinStatement s)
110 {
111 assert(global.errors);
112 result = BE.fallthru;
113 }
114
115 void visitCompound(CompoundStatement cs)
116 {
117 //printf("CompoundStatement.blockExit(%p) %d result = x%X\n", cs, cs.statements.length, result);
118 result = BE.fallthru;
119 Statement slast = null;
120 foreach (s; *cs.statements)
121 {
122 if (!s)
123 continue;
124
125 //printf("result = x%x\n", result);
126 //printf("s: %s\n", s.toChars());
127 if (result & BE.fallthru && slast)
128 {
129 slast = slast.last();
130 if (slast && (slast.isCaseStatement() || slast.isDefaultStatement()) && (s.isCaseStatement() || s.isDefaultStatement()))
131 {
132 // Allow if last case/default was empty
133 CaseStatement sc = slast.isCaseStatement();
134 DefaultStatement sd = slast.isDefaultStatement();
135 auto sl = (sc ? sc.statement : (sd ? sd.statement : null));
136
137 if (sl && (!sl.hasCode() || sl.isErrorStatement()))
138 {
139 }
140 else if (func.getModule().filetype != FileType.c)
141 {
142 const(char)* gototype = s.isCaseStatement() ? "case" : "default";
143 // https://issues.dlang.org/show_bug.cgi?id=22999
144 global.errorSink.error(s.loc, "switch case fallthrough - use 'goto %s;' if intended", gototype);
145 }
146 }
147 }
148
149 if ((result & BE.fallthru) || s.comeFrom())
150 {
151 result &= ~BE.fallthru;
152 result |= blockExit(s, func, eSink);
153 }
154 slast = s;
155 }
156 }
157
158 void visitUnrolledLoop(UnrolledLoopStatement uls)
159 {
160 result = BE.fallthru;
161 foreach (s; *uls.statements)
162 {
163 if (!s)
164 continue;
165 int r = blockExit(s, func, eSink);
166 result |= r & ~(BE.break_ | BE.continue_ | BE.fallthru);
167 if ((r & (BE.fallthru | BE.continue_ | BE.break_)) == 0)
168 result &= ~BE.fallthru;
169 }
170 }
171
172 void visitScope(ScopeStatement s)
173 {
174 //printf("ScopeStatement::blockExit(%p)\n", s.statement);
175 result = blockExit(s.statement, func, eSink);
176 }
177
178 void visitWhile(WhileStatement s)
179 {
180 assert(global.errors);
181 result = BE.fallthru;
182 }
183
184 void visitDo(DoStatement s)
185 {
186 if (s._body)
187 {
188 result = blockExit(s._body, func, eSink);
189 if (result == BE.break_)
190 {
191 result = BE.fallthru;
192 return;
193 }
194 if (result & BE.continue_)
195 result |= BE.fallthru;
196 }
197 else
198 result = BE.fallthru;
199 if (result & BE.fallthru)
200 {
201 result |= canThrow(s.condition, func, eSink);
202
203 if (!(result & BE.break_) && s.condition.toBool().hasValue(true))
204 result &= ~BE.fallthru;
205 }
206 result &= ~(BE.break_ | BE.continue_);
207 }
208
209 void visitFor(ForStatement s)
210 {
211 result = BE.fallthru;
212 if (s._init)
213 {
214 result = blockExit(s._init, func, eSink);
215 if (!(result & BE.fallthru))
216 return;
217 }
218 if (s.condition)
219 {
220 result |= canThrow(s.condition, func, eSink);
221
222 const opt = s.condition.toBool();
223 if (opt.hasValue(true))
224 result &= ~BE.fallthru;
225 else if (opt.hasValue(false))
226 return;
227 }
228 else
229 result &= ~BE.fallthru; // the body must do the exiting
230 if (s._body)
231 {
232 int r = blockExit(s._body, func, eSink);
233 if (r & (BE.break_ | BE.goto_))
234 result |= BE.fallthru;
235 result |= r & ~(BE.fallthru | BE.break_ | BE.continue_);
236 }
237 if (s.increment)
238 result |= canThrow(s.increment, func, eSink);
239 }
240
241 void visitForeach(ForeachStatement s)
242 {
243 result = BE.fallthru;
244 result |= canThrow(s.aggr, func, eSink);
245
246 if (s._body)
247 result |= blockExit(s._body, func, eSink) & ~(BE.break_ | BE.continue_);
248 }
249
250 void visitForeachRange(ForeachRangeStatement s)
251 {
252 assert(global.errors);
253 result = BE.fallthru;
254 }
255
256 void visitIf(IfStatement s)
257 {
258 //printf("IfStatement::blockExit(%p)\n", s);
259 result = BE.none;
260 result |= canThrow(s.condition, func, eSink);
261
262 const opt = s.condition.toBool();
263 if (opt.hasValue(true))
264 {
265 result |= blockExit(s.ifbody, func, eSink);
266 }
267 else if (opt.hasValue(false))
268 {
269 result |= blockExit(s.elsebody, func, eSink);
270 }
271 else
272 {
273 result |= blockExit(s.ifbody, func, eSink);
274 result |= blockExit(s.elsebody, func, eSink);
275 }
276 //printf("IfStatement::blockExit(%p) = x%x\n", s, result);
277 }
278
279 void visitConditional(ConditionalStatement s)
280 {
281 result = blockExit(s.ifbody, func, eSink);
282 if (s.elsebody)
283 result |= blockExit(s.elsebody, func, eSink);
284 }
285
286 void visitPragma(PragmaStatement s)
287 {
288 result = BE.fallthru;
289 }
290
291 void visitStaticAssert(StaticAssertStatement s)
292 {
293 result = BE.fallthru;
294 }
295
296 void visitSwitch(SwitchStatement s)
297 {
298 result = BE.none;
299 result |= canThrow(s.condition, func, eSink);
300
301 if (s._body)
302 {
303 result |= blockExit(s._body, func, eSink);
304 if (result & BE.break_)
305 {
306 result |= BE.fallthru;
307 result &= ~BE.break_;
308 }
309 }
310 else
311 result |= BE.fallthru;
312 }
313
314 void visitCase(CaseStatement s)
315 {
316 result = blockExit(s.statement, func, eSink);
317 }
318
319 void visitDefault(DefaultStatement s)
320 {
321 result = blockExit(s.statement, func, eSink);
322 }
323
324 void visitGotoDefault(GotoDefaultStatement s)
325 {
326 result = BE.goto_;
327 }
328
329 void visitGotoCase(GotoCaseStatement s)
330 {
331 result = BE.goto_;
332 }
333
334 void visitSwitchError(SwitchErrorStatement s)
335 {
336 // Switch errors are non-recoverable
337 result = BE.halt;
338 }
339
340 void visitReturn(ReturnStatement s)
341 {
342 result = BE.return_;
343 if (s.exp)
344 result |= canThrow(s.exp, func, eSink);
345 }
346
347 void visitBreak(BreakStatement s)
348 {
349 //printf("BreakStatement::blockExit(%p) = x%x\n", s, s.ident ? BE.goto_ : BE.break_);
350 result = s.ident ? BE.goto_ : BE.break_;
351 }
352
353 void visitContinue(ContinueStatement s)
354 {
355 result = s.ident ? BE.continue_ | BE.goto_ : BE.continue_;
356 }
357
358 void visitSynchronized(SynchronizedStatement s)
359 {
360 result = blockExit(s._body, func, eSink);
361 }
362
363 void visitWith(WithStatement s)
364 {
365 result = BE.none;
366 result |= canThrow(s.exp, func, eSink);
367 result |= blockExit(s._body, func, eSink);
368 }
369
370 void visitTryCatch(TryCatchStatement s)
371 {
372 assert(s._body);
373 result = blockExit(s._body, func, null);
374
375 int catchresult = 0;
376 foreach (c; *s.catches)
377 {
378 if (c.type == Type.terror)
379 continue;
380
381 int cresult = blockExit(c.handler, func, eSink);
382
383 /* If we're catching Object, then there is no throwing
384 */
385 Identifier id = c.type.toBasetype().isClassHandle().ident;
386 if (c.internalCatch && (cresult & BE.fallthru))
387 {
388 // https://issues.dlang.org/show_bug.cgi?id=11542
389 // leave blockExit flags of the body
390 cresult &= ~BE.fallthru;
391 }
392 else if (id == Id.Object || id == Id.Throwable)
393 {
394 result &= ~(BE.throw_ | BE.errthrow);
395 }
396 else if (id == Id.Exception)
397 {
398 result &= ~BE.throw_;
399 }
400 catchresult |= cresult;
401 }
402 if (eSink && (result & BE.throw_))
403 {
404 // now explain why this is nothrow
405 blockExit(s._body, func, eSink);
406 }
407 result |= catchresult;
408 }
409
410 void visitTryFinally(TryFinallyStatement s)
411 {
412 result = BE.fallthru;
413 if (s._body)
414 result = blockExit(s._body, func, null);
415
416 // check finally body as well, it may throw (bug #4082)
417 int finalresult = BE.fallthru;
418 if (s.finalbody)
419 finalresult = blockExit(s.finalbody, func, null);
420
421 // If either body or finalbody halts
422 if (result == BE.halt)
423 finalresult = BE.none;
424 if (finalresult == BE.halt)
425 result = BE.none;
426
427 if (eSink)
428 {
429 // now explain why this is nothrow
430 if (s._body && (result & BE.throw_))
431 blockExit(s._body, func, eSink);
432 if (s.finalbody && (finalresult & BE.throw_))
433 blockExit(s.finalbody, func, eSink);
434 }
435
436 if (!(finalresult & BE.fallthru))
437 result &= ~BE.fallthru;
438 result |= finalresult & ~BE.fallthru;
439 }
440
441 void visitScopeGuard(ScopeGuardStatement s)
442 {
443 // At this point, this statement is just an empty placeholder
444 result = BE.fallthru;
445 }
446
447 void visitThrow(ThrowStatement s)
448 {
449 if (s.internalThrow)
450 {
451 // https://issues.dlang.org/show_bug.cgi?id=8675
452 // Allow throwing 'Throwable' object even if eSink.
453 result = BE.fallthru;
454 return;
455 }
456
457 result = checkThrow(s.loc, s.exp, func, eSink);
458 }
459
460 void visitGoto(GotoStatement s)
461 {
462 //printf("GotoStatement::blockExit(%p)\n", s);
463 result = BE.goto_;
464 }
465
466 void visitLabel(LabelStatement s)
467 {
468 //printf("LabelStatement::blockExit(%p)\n", s);
469 result = blockExit(s.statement, func, eSink);
470 if (s.breaks)
471 result |= BE.fallthru;
472 }
473
474 void visitCompoundAsm(CompoundAsmStatement s)
475 {
476 // Assume the worst
477 result = BE.fallthru | BE.return_ | BE.goto_ | BE.halt;
478 if (!(s.stc & STC.nothrow_))
479 {
480 if(func)
481 func.setThrow(s.loc, "executing an `asm` statement without a `nothrow` annotation");
482 if (eSink)
483 eSink.error(s.loc, "`asm` statement is assumed to throw - mark it with `nothrow` if it does not"); // TODO
484 else
485 result |= BE.throw_;
486 }
487 }
488
489 void visitImport(ImportStatement s)
490 {
491 result = BE.fallthru;
492 }
493
494 if (!s)
495 return BE.fallthru;
496 mixin VisitStatement!void visit;
497 visit.VisitStatement(s);
498 return result;
499 }
500
501 /++
502 + Checks whether `throw <exp>` throws an `Exception` or an `Error`
503 + and raises an error if this violates `nothrow`.
504 +
505 + Params:
506 + loc = location of the `throw`
507 + exp = expression yielding the throwable
508 + eSink = if !null then inside of a `nothrow` scope
509 + func = function containing the `throw`
510 +
511 + Returns: `BE.[err]throw` depending on the type of `exp`
512 +/
513 BE checkThrow(Loc loc, Expression exp, FuncDeclaration func, ErrorSink eSink)
514 {
515 Type t = exp.type.toBasetype();
516 ClassDeclaration cd = t.isClassHandle();
517 assert(cd);
518
519 if (cd.isErrorException())
520 {
521 return BE.errthrow;
522 }
523 if (eSink)
524 eSink.error(loc, "`%s` is thrown but not caught", exp.type.toChars());
525 else if (func)
526 func.setThrow(loc, "`%s` being thrown but not caught", exp.type);
527
528 return BE.throw_;
529 }