1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 14:50:45 +00:00
Commit Graph

53 Commits

Author SHA1 Message Date
Andreas Kling
6b2b90d2b0 AK: Remove AK_HAS_CONDITIONALLY_TRIVIAL
Code behind this appears to compile nicely with Clang 17 and later.
2024-05-10 15:03:24 +00:00
Timothy Flynn
d878975f95 AK+LibJS: Remove OFFSET_OF and its users
With the LibJS JIT removed, let's not expose pointers to internal
members.
2024-02-29 09:00:00 +01:00
Lucas CHOLLET
aaf54f8cf8 AK: Allow Optional<T&> to be constructed by OptionalNone()
This is an extension of cc0b970d but for the reference-handling
specialization of Optional.

This basically allow us to write code like:
```cpp
Optional<u8&> opt;
opt = OptionalNone{};
```
2023-11-29 02:19:41 +03:30
Andreas Kling
bdce36dddb AK: Expose memory offset of Optional's internal fields 2023-11-07 11:33:04 +01:00
Timothy Flynn
0a1874c203 AK: Export the OptionalNone helper structure 2023-01-16 10:12:37 +00:00
Ali Mohammad Pur
96b36203a2 AK: Add Optional::lazy_emplace(Callable)
This makes it possible to emplace using a given function instead of
passing constructor arguments.
2022-12-11 20:44:54 +03:30
Ali Mohammad Pur
cc0b970d81 AK: Allow Optional<T> to be constructed by OptionalNone()
This is needed by the Jakt runtime too.
2022-12-11 20:44:54 +03:30
Ali Mohammad Pur
21c2d8bd98 AK: Add a Optional::value_or_lazy_evaluated(constructor_function) API
This allows the user to avoid constructing the default value if the
optional already contains a value.
This is used by the Jakt runtime.
2022-12-11 20:44:54 +03:30
Ali Mohammad Pur
cc948d06a6 AK: Add a functional-style Optional::map(mapper_function) API
This allows the user to transform the contents of the optional (if any
exists), without manually unwrapping and then rewrapping it.
This is needed by the Jakt runtime.
2022-12-11 20:44:54 +03:30
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Andreas Kling
ae3ffdd521 AK: Make it possible to not using AK classes into the global namespace
This patch adds the `USING_AK_GLOBALLY` macro which is enabled by
default, but can be overridden by build flags.

This is a step towards integrating Jakt and AK types.
2022-11-26 15:51:34 +01:00
Ali Mohammad Pur
221ecf17d3 AK: Allow Optional<T&> to exist
This implements Optional<T&> as a T*, whose presence has been missing
since the early days of Optional.
As a lot of find_foo() APIs return an Optional<T> which imposes a
pointless copy on the underlying value, and can sometimes be very
misleading, with this change, those APIs can return Optional<T&>.
2022-04-04 12:48:31 +02:00
Idan Horowitz
a3a4d0aea2 AK: Make Optional<T> explicitly constructible from Optional<U>
As long as T is constructible from U. This allows us to avoid patterns
like `abc.has_value() ? Optional<U>(abc.value()) : Optional<U>()`.
2022-01-23 18:53:42 +02:00
Andreas Kling
b4c0e27d26 AK: Always inline Optional::release_value()
This allows the compiler to optimize away the VERIFY(m_has_value) in
many cases.
2021-10-25 14:31:46 +02:00
sin-ack
f633fb706e AK: Add note about an internal compile error with Optional in GCC 10.3+
This bit me because I accidentally made the destructor for a class which
was wrapped in an Optional private. This causes none of the Optional
destructors to be able to be deduced, which when combined with concepts
causes an internal compile error in GCC 10.3.0+. This commit adds a note
here to make sure that future encounters of this bug does not surprise
people.
2021-09-08 00:37:19 +02:00
Andreas Kling
7dda773426 AK: Add rvalue-ref qualifiers for Optional's value() and value_or()
This avoids a value copy when calling value() or value_or() on a
temporary Optional. This is very common when using the HashMap::get()
API like this:

    auto value = hash_map.get(key).value_or(fallback_value);
2021-09-04 03:02:08 +02:00
Andreas Kling
0b36499f46 AK: Convert Optional.h to east-const style 2021-09-04 03:02:08 +02:00
Daniel Bertalan
515e2d9734 AK: Use conditionally trivial special member functions
This commit makes use of the conditionally trivial special member
functions introduced in C++20. Basically, `Optional` and `Variant`
inherits whether its wrapped type is trivially copy constructible,
trivially copy assignable or trivially destructible. This lets the
compiler optimize optimize a large number of their use cases.

The constraints have been applied to `Optional`'s converting
constructors too in order to make the API more explicit.

This feature is not supported by Clang yet, so we use conditional
compilation so that Lagom can be built on macOS. Once Clang has P0848R3
support, these can be removed.
2021-07-04 07:24:41 +04:30
Daniel Bertalan
b9f30c6f2a Everywhere: Fix some alignment issues
When creating uninitialized storage for variables, we need to make sure
that the alignment is correct. Fixes a KUBSAN failure when running
kernels compiled with Clang.

In `Syscalls/socket.cpp`, we can simply use local variables, as
`sockaddr_un` is a POD type.

Along with moving the `alignas` specifier to the correct member,
`AK::Optional`'s internal buffer has been made non-zeroed by default.
GCC emitted bogus uninitialized memory access warnings, so we now use
`__builtin_launder` to tell the compiler that we know what we are doing.
This might disable some optimizations, but judging by how GCC failed to
notice that the memory's initialization is dependent on `m_has_value`,
I'm not sure that's a bad thing.
2021-07-03 01:56:31 +04:30
Max Wipfli
f3fda59abd AK: Enable direct comparsion of Optional<T> and T
This patch introduces a new operator== to compare an Optional to its
contained type directly. If the Optional does not contain a value, the
comparison will always return false.

This also adds a test case for the new behavior as well as comparison
between Optional objects themselves.
2021-06-01 11:38:17 +02:00
Itamar
1da0d402b7 AK: Add constructors to Optional that accept non const qualified inputs 2021-05-08 18:10:56 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Timothy Flynn
99e1d8a359 AK: Add type alias for AK::Optional 2021-04-16 19:19:31 +02:00
Andreas Kling
5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
Andreas Kling
145923bdc9 AK: Slap Optional with the ALWAYS_INLINE stick
I saw some Optional constructors when profiling the dynamic loader
and that seemed silly since we can inline them at no/little cost.
2021-02-23 17:42:05 +01:00
Brian Gianforcaro
3356f438ca AK: Mark Optional getters as [[nodiscard]]
There is no reason to call a getter without observing the result, doing
so indicates an error in the code. Mark these methods as [[nodiscard]]
to find these cases.
2021-02-15 09:34:52 +01:00
Ben Wiederhake
cc89606ba8 Everywhere: Remove unnecessary headers 3/4
Arbitrarily split up to make git bisect easier.

These unnecessary #include's were found by combining an automated tool (which
determined likely candidates) and some brain power (which decided whether
the #include is also semantically superfluous).
2021-02-08 18:03:57 +01:00
Lenny Maiorani
e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
Linus Groh
bbe787a0af Everywhere: Re-format with clang-format-11
Compared to version 10 this fixes a bunch of formatting issues, mostly
around structs/classes with attributes like [[gnu::packed]], and
incorrect insertion of spaces in parameter types ("T &"/"T &&").
I also removed a bunch of // clang-format off/on and FIXME comments that
are no longer relevant - on the other hand it tried to destroy a couple of
neatly formatted comments, so I had to add some as well.
2020-12-31 21:51:00 +01:00
asynts
a7c014125f AK: Add operator* and operator-> overloads in Optional. 2020-12-31 00:51:12 +01:00
Lenny Maiorani
7d8a9bdb1e AK: Cleanup missing includes and #ifdef evaluation
Problem:
- Several files have missing includes. This results in complaints from
  `clang-tidy`.
- `#ifdef` is followed by `#elif <value>` which evaluates to `0`.

Solution:
- Add missing includes.
- Change to `#elif defined(<value>)`.
2020-11-22 11:35:53 +01:00
asynts
deb85c47b5 AK: Add Optional::emplace method. 2020-08-30 09:56:10 +02:00
Peter Elliott
c68537271c AK: Add operator== to AK::Optional
The semantics:
- two Optionals are equal if they are both None
- two Optionals are equal if they are both Some, and their values are
  operator==
2020-08-23 01:05:22 +02:00
Brian Gianforcaro
a7a7e6245f AK: Decorate Optional<T> with [[nodisard]] 2020-08-05 12:27:15 +02:00
Andreas Kling
76bcd284f9 AK: Remove experimental clang -Wconsumed stuff
This stopped working quite some time ago due to Clang losing track of
typestates for some reason and everything becoming "unknown".

Since we're primarily using GCC anyway, it doesn't seem worth it to try
and maintain this non-working experiment for a secondary compiler.

Also it doesn't look like the Clang team is actively maintaining this
flag anyway. So good-bye, -Wconsumed. :/
2020-05-16 10:55:54 +02:00
Andreas Kling
888e35f0fe AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macros
It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
2020-04-30 11:43:25 +02:00
Andreas Kling
26a8984d03 AK: Inline Optional functions more aggressively
This turns into much less code in the most common cases, here's why:
The normal Optional usage pattern is something like:

    auto foo = get_me_an_optional();
    if (foo.has_value())
        do_stuff_with(foo.value());

In this typical scenario, we check has_value() before calling value().
Without inlining, value() will double-check has_value() itself and
assert if it fails. Inlining allows the compiler to optimize all of
this away.
2020-04-12 20:37:51 +02:00
Andreas Kling
900f51ccd0 AK: Move memory stuff (fast memcpy, etc) to a separate header
Move the "fast memcpy" stuff out of StdLibExtras.h and into Memory.h.
This will break a ton of things that were relying on StdLibExtras.h
to include a bunch of other headers. Fix will follow immediately after.

This makes it possible to include StdLibExtras.h from Types.h, which is
the main point of this exercise.
2020-03-08 13:06:51 +01:00
Andreas Kling
35d88f536c AK: Use __builtin_memset() and such to reduce header dependencies
We can use __builtin_memset() without including <string.h>.
This is pretty neat, as it will allow us to reduce the header deps
of AK templates a bit, if applied consistently.

Note that this is an enabling change for an upcoming #include removal.
2020-03-08 13:06:51 +01:00
Andreas Kling
8bb361889c AK: Remove Optional::operator bool()
This was causing some obvious-in-hindsight but hard to spot bugs where
we'd implicitly convert the bool to an integer type and carry on with
the number 1 instead of the actual value().
2020-03-06 10:32:58 +01:00
Andreas Kling
17846dd063 AK: Zero-initialize the internal storage of Optional 2020-02-24 10:22:27 +01:00
Andreas Kling
8f7333f080 LibCore: Add a forward declaration header
This patch adds <LibCore/Forward.h> and uses it in various places to
shrink the header dependency graph.
2020-02-14 23:31:18 +01:00
Andreas Kling
939a605334 AK: Add missing StdLibExtras.h include in Optional.h 2020-02-06 11:55:19 +01:00
Andreas Kling
94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Andreas Kling
a00419ed77 AK: Optional::operator bool() should consume the Optional
We use consumable annotations to catch bugs where you get the .value()
of an Optional before verifying that it's okay.

The bug here was that only has_value() would set the consumed state,
even though operator bool() does the same job.
2019-08-25 06:45:09 +02:00
Andreas Kling
865a1b913c AK: Add Optional<T>(const U&)
This replaces Optional<T>(U&&) which clang-tidy complained may hide the
regular copy and move constructors. That's a good point, clang-tidy,
and I appreciate you pointing that out!
2019-08-08 18:34:59 +02:00
Andreas Kling
60c25228ee AK: Fix -Wconsumed warnings in Optional move-ctor and move-assign
Our protocol says we have to call has_value() before release_value().
The code was already safe, but the compiler had no way of knowing that.
2019-08-07 07:17:52 +02:00
Andreas Kling
151e6a1818 AK: Fix leak in Optional(Optional&&)
We were *copying* the other Optional's value and then marking it as not
having a value. This prevented us from ever destroying the original.
2019-08-05 22:27:47 +02:00
Andreas Kling
9553ecfe01 AK: Optional::operator=(Optional&&) should clear movee's has_value bit
We were forgetting to clear m_has_value in the Optional being moved
from when using operator=(Optional&&).
2019-08-05 21:47:36 +02:00
Robin Burchell
28362fcc57 Optional: Add consumable checks
These will, when building with clang, prevent using Optional::value
without first checking Optional::has_value() - at compile time.
2019-07-31 09:06:39 +02:00