]> git.ipfire.org Git - people/ms/gcc.git/blob - gcc/d/dmd/canthrow.d
d: Merge upstream dmd, druntime 4ca4140e58, phobos 454dff14d.
[people/ms/gcc.git] / gcc / d / dmd / canthrow.d
1 /**
2 * Perform checks for `nothrow`.
3 *
4 * Specification: $(LINK2 https://dlang.org/spec/function.html#nothrow-functions, Nothrow Functions)
5 *
6 * Copyright: Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/canthrow.d, _canthrow.d)
10 * Documentation: https://dlang.org/phobos/dmd_canthrow.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/canthrow.d
12 */
13
14 module dmd.canthrow;
15
16 import dmd.aggregate;
17 import dmd.apply;
18 import dmd.arraytypes;
19 import dmd.attrib;
20 import dmd.astenums;
21 import dmd.blockexit : BE, checkThrow;
22 import dmd.declaration;
23 import dmd.dsymbol;
24 import dmd.expression;
25 import dmd.func;
26 import dmd.globals;
27 import dmd.init;
28 import dmd.mtype;
29 import dmd.root.rootobject;
30 import dmd.tokens;
31 import dmd.visitor;
32
33 /**
34 * Status indicating what kind of throwable might be caused by an expression.
35 *
36 * This is a subset of `BE` restricted to the values actually used by `canThrow`.
37 */
38 enum CT : BE
39 {
40 /// Never throws an `Exception` or `Throwable`
41 none = BE.none,
42
43 /// Might throw an `Exception`
44 exception = BE.throw_,
45
46 // Might throw an `Error`
47 error = BE.errthrow,
48 }
49
50 /********************************************
51 * Returns true if the expression may throw exceptions.
52 * If 'mustNotThrow' is true, generate an error if it throws
53 */
54 extern (C++) /* CT */ BE canThrow(Expression e, FuncDeclaration func, bool mustNotThrow)
55 {
56 //printf("Expression::canThrow(%d) %s\n", mustNotThrow, toChars());
57 // stop walking if we determine this expression can throw
58 extern (C++) final class CanThrow : StoppableVisitor
59 {
60 alias visit = typeof(super).visit;
61 FuncDeclaration func;
62 bool mustNotThrow;
63 CT result;
64
65 public:
66 extern (D) this(FuncDeclaration func, bool mustNotThrow) scope
67 {
68 this.func = func;
69 this.mustNotThrow = mustNotThrow;
70 }
71
72 void checkFuncThrows(Expression e, FuncDeclaration f)
73 {
74 auto tf = f.type.toBasetype().isTypeFunction();
75 if (tf && !tf.isnothrow)
76 {
77 if (mustNotThrow)
78 {
79 e.error("%s `%s` is not `nothrow`",
80 f.kind(), f.toPrettyChars());
81
82 e.checkOverridenDtor(null, f, dd => dd.type.toTypeFunction().isnothrow, "not nothrow");
83 }
84 result |= CT.exception;
85 }
86 }
87
88 override void visit(Expression)
89 {
90 }
91
92 override void visit(DeclarationExp de)
93 {
94 result |= Dsymbol_canThrow(de.declaration, func, mustNotThrow);
95 }
96
97 override void visit(CallExp ce)
98 {
99 if (ce.inDebugStatement)
100 return;
101
102 if (global.errors && !ce.e1.type)
103 return; // error recovery
104
105 if (ce.f && ce.arguments.length > 0)
106 {
107 Type tb = (*ce.arguments)[0].type.toBasetype();
108 auto tbNext = tb.nextOf();
109 if (tbNext)
110 {
111 auto ts = tbNext.baseElemOf().isTypeStruct();
112 if (ts)
113 {
114 auto sd = ts.sym;
115 const id = ce.f.ident;
116 if (sd.postblit && isArrayConstructionOrAssign(id))
117 {
118 checkFuncThrows(ce, sd.postblit);
119 return;
120 }
121 }
122 }
123 }
124
125 /* If calling a function or delegate that is typed as nothrow,
126 * then this expression cannot throw.
127 * Note that pure functions can throw.
128 */
129 if (ce.f && ce.f == func)
130 return;
131 Type t = ce.e1.type.toBasetype();
132 auto tf = t.isTypeFunction();
133 if (tf && tf.isnothrow)
134 return;
135 else
136 {
137 auto td = t.isTypeDelegate();
138 if (td && td.nextOf().isTypeFunction().isnothrow)
139 return;
140 }
141
142 if (ce.f)
143 checkFuncThrows(ce, ce.f);
144 else if (mustNotThrow)
145 {
146 auto e1 = ce.e1;
147 if (auto pe = e1.isPtrExp()) // print 'fp' if e1 is (*fp)
148 e1 = pe.e1;
149 ce.error("`%s` is not `nothrow`", e1.toChars());
150 }
151 result |= CT.exception;
152 }
153
154 override void visit(NewExp ne)
155 {
156 if (ne.member)
157 {
158 // See if constructor call can throw
159 checkFuncThrows(ne, ne.member);
160 }
161 // regard storage allocation failures as not recoverable
162 }
163
164 override void visit(DeleteExp de)
165 {
166 Type tb = de.e1.type.toBasetype();
167 AggregateDeclaration ad = null;
168 switch (tb.ty)
169 {
170 case Tclass:
171 ad = tb.isTypeClass().sym;
172 break;
173
174 default:
175 assert(0); // error should have been detected by semantic()
176 }
177
178 if (ad.dtor)
179 checkFuncThrows(de, ad.dtor);
180 }
181
182 override void visit(AssignExp ae)
183 {
184 // blit-init cannot throw
185 if (ae.op == EXP.blit)
186 return;
187 /* Element-wise assignment could invoke postblits.
188 */
189 Type t;
190 if (ae.type.toBasetype().ty == Tsarray)
191 {
192 if (!ae.e2.isLvalue())
193 return;
194 t = ae.type;
195 }
196 else if (auto se = ae.e1.isSliceExp())
197 t = se.e1.type;
198 else
199 return;
200
201 if (auto ts = t.baseElemOf().isTypeStruct())
202 if (auto postblit = ts.sym.postblit)
203 checkFuncThrows(ae, postblit);
204 }
205
206 override void visit(ThrowExp te)
207 {
208 const res = checkThrow(te.loc, te.e1, mustNotThrow);
209 assert((res & ~(CT.exception | CT.error)) == 0);
210 result |= res;
211 }
212
213 override void visit(NewAnonClassExp)
214 {
215 assert(0); // should have been lowered by semantic()
216 }
217 }
218
219 scope CanThrow ct = new CanThrow(func, mustNotThrow);
220 walkPostorder(e, ct);
221 return ct.result;
222 }
223
224 /**************************************
225 * Does symbol, when initialized, throw?
226 * Mirrors logic in Dsymbol_toElem().
227 */
228 private CT Dsymbol_canThrow(Dsymbol s, FuncDeclaration func, bool mustNotThrow)
229 {
230 CT result;
231
232 int symbolDg(Dsymbol s)
233 {
234 result |= Dsymbol_canThrow(s, func, mustNotThrow);
235 return 0;
236 }
237
238 //printf("Dsymbol_toElem() %s\n", s.toChars());
239 if (auto vd = s.isVarDeclaration())
240 {
241 s = s.toAlias();
242 if (s != vd)
243 return Dsymbol_canThrow(s, func, mustNotThrow);
244 if (vd.storage_class & STC.manifest)
245 {
246 }
247 else if (vd.isStatic() || vd.storage_class & (STC.extern_ | STC.gshared))
248 {
249 }
250 else
251 {
252 if (vd._init)
253 {
254 if (auto ie = vd._init.isExpInitializer())
255 result |= canThrow(ie.exp, func, mustNotThrow);
256 }
257 if (vd.needsScopeDtor())
258 result |= canThrow(vd.edtor, func, mustNotThrow);
259 }
260 }
261 else if (auto ad = s.isAttribDeclaration())
262 {
263 ad.include(null).foreachDsymbol(&symbolDg);
264 }
265 else if (auto tm = s.isTemplateMixin())
266 {
267 tm.members.foreachDsymbol(&symbolDg);
268 }
269 else if (auto td = s.isTupleDeclaration())
270 {
271 td.foreachVar(&symbolDg);
272 }
273 return result;
274 }