summaryrefslogtreecommitdiff
path: root/gcc/d/dmd/common
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/d/dmd/common')
-rw-r--r--gcc/d/dmd/common/README.md1
-rw-r--r--gcc/d/dmd/common/bitfields.d70
-rw-r--r--gcc/d/dmd/common/file.d4
-rw-r--r--gcc/d/dmd/common/outbuffer.d4
-rw-r--r--gcc/d/dmd/common/string.d4
5 files changed, 83 insertions, 0 deletions
diff --git a/gcc/d/dmd/common/README.md b/gcc/d/dmd/common/README.md
index a9b65c3dd8d..fb282dcf0a1 100644
--- a/gcc/d/dmd/common/README.md
+++ b/gcc/d/dmd/common/README.md
@@ -2,6 +2,7 @@
| File | Purpose |
|------------------------------------------------------------------------------------|-----------------------------------------------------------------|
+| [bitfields.d](https://github.com/dlang/dmd/blob/master/src/dmd/common/bitfields.d) | Pack multiple boolean fields into bit fields |
| [file.d](https://github.com/dlang/dmd/blob/master/src/dmd/common/file.d) | Functions and objects dedicated to file I/O and management |
| [outbuffer.d](https://github.com/dlang/dmd/blob/master/src/dmd/common/outbuffer.d) | An expandable buffer in which you can write text or binary data |
| [string.d](https://github.com/dlang/dmd/blob/master/src/dmd/common/string.d) | Common string functions including filename manipulation |
diff --git a/gcc/d/dmd/common/bitfields.d b/gcc/d/dmd/common/bitfields.d
new file mode 100644
index 00000000000..d17983d66b4
--- /dev/null
+++ b/gcc/d/dmd/common/bitfields.d
@@ -0,0 +1,70 @@
+/**
+ * A library bitfields utility
+ *
+ * Copyright: Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
+ * Authors: Dennis Korpel
+ * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
+ * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/common/bitfields.d, common/bitfields.d)
+ * Documentation: https://dlang.org/phobos/dmd_common_bitfields.html
+ * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/common/bitfields.d
+ */
+module dmd.common.bitfields;
+
+/**
+ * Generate code for bit fields inside a struct/class body
+ * Params:
+ * S = type of a struct with only boolean fields, which should become bit fields
+ * T = type of bit fields variable, must have enough bits to store all booleans
+ * Returns: D code with a bit fields variable and getter / setter functions
+ */
+extern (D) string generateBitFields(S, T)()
+if (__traits(isUnsigned, T))
+{
+ string result = "extern (C++) pure nothrow @nogc @safe final {";
+ enum structName = __traits(identifier, S);
+
+ foreach (size_t i, mem; __traits(allMembers, S))
+ {
+ static assert(is(typeof(__traits(getMember, S, mem)) == bool));
+ static assert(i < T.sizeof * 8, "too many fields for bit field storage of type `"~T.stringof~"`");
+ enum mask = "(1 << "~i.stringof~")";
+ result ~= "
+ /// set or get the corresponding "~structName~" member
+ bool "~mem~"() const { return !!(bitFields & "~mask~"); }
+ /// ditto
+ bool "~mem~"(bool v)
+ {
+ v ? (bitFields |= "~mask~") : (bitFields &= ~"~mask~");
+ return v;
+ }";
+ }
+ return result ~ "}\n private "~T.stringof~" bitFields;\n";
+}
+
+///
+unittest
+{
+ static struct B
+ {
+ bool x;
+ bool y;
+ bool z;
+ }
+
+ static struct S
+ {
+ mixin(generateBitFields!(B, ubyte));
+ }
+
+ S s;
+ assert(!s.x);
+ s.x = true;
+ assert(s.x);
+ s.x = false;
+ assert(!s.x);
+
+ s.y = true;
+ assert(s.y);
+ assert(!s.x);
+ assert(!s.z);
+}
diff --git a/gcc/d/dmd/common/file.d b/gcc/d/dmd/common/file.d
index e4483d5f947..8f34b5319a9 100644
--- a/gcc/d/dmd/common/file.d
+++ b/gcc/d/dmd/common/file.d
@@ -25,6 +25,8 @@ import core.sys.posix.unistd;
import dmd.common.string;
+nothrow:
+
/**
Encapsulated management of a memory-mapped file.
@@ -52,6 +54,8 @@ struct FileMapping(Datum)
private const(char)* name;
// state }
+ nothrow:
+
/**
Open `filename` and map it in memory. If `Datum` is `const`, opens for
read-only and maps the content in memory; no error is issued if the file
diff --git a/gcc/d/dmd/common/outbuffer.d b/gcc/d/dmd/common/outbuffer.d
index 0705c1880c0..7e46d294f3d 100644
--- a/gcc/d/dmd/common/outbuffer.d
+++ b/gcc/d/dmd/common/outbuffer.d
@@ -16,6 +16,8 @@ import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
+nothrow:
+
// In theory these functions should also restore errno, but we don't care because
// we abort application on error anyway.
extern (C) private pure @system @nogc nothrow
@@ -54,6 +56,8 @@ struct OutBuffer
int level;
// state }
+ nothrow:
+
/**
Construct given size.
*/
diff --git a/gcc/d/dmd/common/string.d b/gcc/d/dmd/common/string.d
index d3bc24f2667..48bf9bb5b55 100644
--- a/gcc/d/dmd/common/string.d
+++ b/gcc/d/dmd/common/string.d
@@ -10,6 +10,8 @@
*/
module dmd.common.string;
+nothrow:
+
/**
Defines a temporary array using a fixed-length buffer as back store. If the length
of the buffer suffices, it is readily used. Otherwise, `malloc` is used to
@@ -26,6 +28,8 @@ struct SmallBuffer(T)
private T[] _extent;
private bool needsFree;
+ nothrow:
+
@disable this(); // no default ctor
@disable this(ref const SmallBuffer!T); // noncopyable, nonassignable