Bug Summary

File:build/source/llvm/include/llvm/ADT/Optional.h
Warning:line 183, column 13
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name WebAssemblyAsmTypeCheck.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -I lib/Target/WebAssembly/AsmParser -I /build/source/llvm/lib/Target/WebAssembly/AsmParser -I /build/source/llvm/lib/Target/WebAssembly -I lib/Target/WebAssembly -I include -I /build/source/llvm/include -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1670066131 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility=hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-12-03-132955-15984-1 -x c++ /build/source/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp

/build/source/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp

1//==- WebAssemblyAsmTypeCheck.cpp - Assembler for WebAssembly -*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file is part of the WebAssembly Assembler.
11///
12/// It contains code to translate a parsed .s file into MCInsts.
13///
14//===----------------------------------------------------------------------===//
15
16#include "AsmParser/WebAssemblyAsmTypeCheck.h"
17#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
19#include "TargetInfo/WebAssemblyTargetInfo.h"
20#include "Utils/WebAssemblyTypeUtilities.h"
21#include "Utils/WebAssemblyUtilities.h"
22#include "WebAssembly.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCInst.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
28#include "llvm/MC/MCParser/MCTargetAsmParser.h"
29#include "llvm/MC/MCSectionWasm.h"
30#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSubtargetInfo.h"
32#include "llvm/MC/MCSymbol.h"
33#include "llvm/MC/MCSymbolWasm.h"
34#include "llvm/MC/TargetRegistry.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Endian.h"
37#include "llvm/Support/SourceMgr.h"
38
39using namespace llvm;
40
41#define DEBUG_TYPE"wasm-asm-parser" "wasm-asm-parser"
42
43extern StringRef GetMnemonic(unsigned Opc);
44
45namespace llvm {
46
47WebAssemblyAsmTypeCheck::WebAssemblyAsmTypeCheck(MCAsmParser &Parser,
48 const MCInstrInfo &MII, bool is64)
49 : Parser(Parser), MII(MII), is64(is64) {
50}
51
52void WebAssemblyAsmTypeCheck::funcDecl(const wasm::WasmSignature &Sig) {
53 LocalTypes.assign(Sig.Params.begin(), Sig.Params.end());
54 ReturnTypes.assign(Sig.Returns.begin(), Sig.Returns.end());
55}
56
57void WebAssemblyAsmTypeCheck::localDecl(const SmallVector<wasm::ValType, 4> &Locals) {
58 LocalTypes.insert(LocalTypes.end(), Locals.begin(), Locals.end());
59}
60
61void WebAssemblyAsmTypeCheck::dumpTypeStack(Twine Msg) {
62 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
63 std::string s;do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
64 for (auto VT : Stack) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
65 s += WebAssembly::typeToString(VT);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
66 s += " ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
67 }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
68 dbgs() << Msg << s << '\n';do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
69 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("wasm-asm-parser")) { { std::string s; for (auto VT : Stack)
{ s += WebAssembly::typeToString(VT); s += " "; } dbgs() <<
Msg << s << '\n'; }; } } while (false)
;
70}
71
72bool WebAssemblyAsmTypeCheck::typeError(SMLoc ErrorLoc, const Twine &Msg) {
73 // Once you get one type error in a function, it will likely trigger more
74 // which are mostly not helpful.
75 if (TypeErrorThisFunction)
76 return true;
77 // If we're currently in unreachable code, we suppress errors completely.
78 if (Unreachable)
79 return false;
80 TypeErrorThisFunction = true;
81 dumpTypeStack("current stack: ");
82 return Parser.Error(ErrorLoc, Msg);
83}
84
85bool WebAssemblyAsmTypeCheck::popType(SMLoc ErrorLoc,
86 Optional<wasm::ValType> EVT) {
87 if (Stack.empty()) {
88 return typeError(ErrorLoc,
89 EVT ? StringRef("empty stack while popping ") +
90 WebAssembly::typeToString(EVT.value())
91 : StringRef("empty stack while popping value"));
92 }
93 auto PVT = Stack.pop_back_val();
94 if (EVT && EVT.value() != PVT) {
95 return typeError(
96 ErrorLoc, StringRef("popped ") + WebAssembly::typeToString(PVT) +
97 ", expected " + WebAssembly::typeToString(EVT.value()));
98 }
99 return false;
100}
101
102bool WebAssemblyAsmTypeCheck::popRefType(SMLoc ErrorLoc) {
103 if (Stack.empty()) {
104 return typeError(ErrorLoc, StringRef("empty stack while popping reftype"));
105 }
106 auto PVT = Stack.pop_back_val();
107 if (!WebAssembly::isRefType(PVT)) {
108 return typeError(ErrorLoc, StringRef("popped ") +
109 WebAssembly::typeToString(PVT) +
110 ", expected reftype");
111 }
112 return false;
113}
114
115bool WebAssemblyAsmTypeCheck::getLocal(SMLoc ErrorLoc, const MCInst &Inst,
116 wasm::ValType &Type) {
117 auto Local = static_cast<size_t>(Inst.getOperand(0).getImm());
118 if (Local >= LocalTypes.size())
119 return typeError(ErrorLoc, StringRef("no local type specified for index ") +
120 std::to_string(Local));
121 Type = LocalTypes[Local];
122 return false;
123}
124
125bool WebAssemblyAsmTypeCheck::checkEnd(SMLoc ErrorLoc, bool PopVals) {
126 if (LastSig.Returns.size() > Stack.size())
127 return typeError(ErrorLoc, "end: insufficient values on the type stack");
128
129 if (PopVals) {
130 for (auto VT : llvm::reverse(LastSig.Returns)) {
131 if (popType(ErrorLoc, VT))
132 return true;
133 }
134 return false;
135 }
136
137 for (size_t i = 0; i < LastSig.Returns.size(); i++) {
138 auto EVT = LastSig.Returns[i];
139 auto PVT = Stack[Stack.size() - LastSig.Returns.size() + i];
140 if (PVT != EVT)
141 return typeError(
142 ErrorLoc, StringRef("end got ") + WebAssembly::typeToString(PVT) +
143 ", expected " + WebAssembly::typeToString(EVT));
144 }
145 return false;
146}
147
148bool WebAssemblyAsmTypeCheck::checkSig(SMLoc ErrorLoc,
149 const wasm::WasmSignature& Sig) {
150 for (auto VT : llvm::reverse(Sig.Params))
151 if (popType(ErrorLoc, VT)) return true;
152 Stack.insert(Stack.end(), Sig.Returns.begin(), Sig.Returns.end());
153 return false;
154}
155
156bool WebAssemblyAsmTypeCheck::getSymRef(SMLoc ErrorLoc, const MCInst &Inst,
157 const MCSymbolRefExpr *&SymRef) {
158 auto Op = Inst.getOperand(0);
159 if (!Op.isExpr())
160 return typeError(ErrorLoc, StringRef("expected expression operand"));
161 SymRef = dyn_cast<MCSymbolRefExpr>(Op.getExpr());
162 if (!SymRef)
163 return typeError(ErrorLoc, StringRef("expected symbol operand"));
164 return false;
165}
166
167bool WebAssemblyAsmTypeCheck::getGlobal(SMLoc ErrorLoc, const MCInst &Inst,
168 wasm::ValType &Type) {
169 const MCSymbolRefExpr *SymRef;
170 if (getSymRef(ErrorLoc, Inst, SymRef))
171 return true;
172 auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
173 switch (WasmSym->getType().value_or(wasm::WASM_SYMBOL_TYPE_DATA)) {
174 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
175 Type = static_cast<wasm::ValType>(WasmSym->getGlobalType().Type);
176 break;
177 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
178 case wasm::WASM_SYMBOL_TYPE_DATA:
179 switch (SymRef->getKind()) {
180 case MCSymbolRefExpr::VK_GOT:
181 case MCSymbolRefExpr::VK_WASM_GOT_TLS:
182 Type = is64 ? wasm::ValType::I64 : wasm::ValType::I32;
183 return false;
184 default:
185 break;
186 }
187 [[fallthrough]];
188 default:
189 return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
190 " missing .globaltype");
191 }
192 return false;
193}
194
195bool WebAssemblyAsmTypeCheck::getTable(SMLoc ErrorLoc, const MCInst &Inst,
196 wasm::ValType &Type) {
197 const MCSymbolRefExpr *SymRef;
198 if (getSymRef(ErrorLoc, Inst, SymRef))
199 return true;
200 auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
201 if (WasmSym->getType().value_or(wasm::WASM_SYMBOL_TYPE_DATA) !=
202 wasm::WASM_SYMBOL_TYPE_TABLE)
203 return typeError(ErrorLoc, StringRef("symbol ") + WasmSym->getName() +
204 " missing .tabletype");
205 Type = static_cast<wasm::ValType>(WasmSym->getTableType().ElemType);
206 return false;
207}
208
209bool WebAssemblyAsmTypeCheck::endOfFunction(SMLoc ErrorLoc) {
210 // Check the return types.
211 for (auto RVT : llvm::reverse(ReturnTypes)) {
212 if (popType(ErrorLoc, RVT))
213 return true;
214 }
215 if (!Stack.empty()) {
216 return typeError(ErrorLoc, std::to_string(Stack.size()) +
217 " superfluous return values");
218 }
219 Unreachable = true;
220 return false;
221}
222
223bool WebAssemblyAsmTypeCheck::typeCheck(SMLoc ErrorLoc, const MCInst &Inst,
224 OperandVector &Operands) {
225 auto Opc = Inst.getOpcode();
226 auto Name = GetMnemonic(Opc);
227 dumpTypeStack("typechecking " + Name + ": ");
228 wasm::ValType Type;
229 if (Name == "local.get") {
1
Assuming the condition is false
2
Taking false branch
230 if (getLocal(Operands[1]->getStartLoc(), Inst, Type))
231 return true;
232 Stack.push_back(Type);
233 } else if (Name == "local.set") {
3
Assuming the condition is true
4
Taking true branch
234 if (getLocal(Operands[1]->getStartLoc(), Inst, Type))
5
Taking false branch
235 return true;
236 if (popType(ErrorLoc, Type))
6
Calling constructor for 'Optional<llvm::wasm::ValType>'
237 return true;
238 } else if (Name == "local.tee") {
239 if (getLocal(Operands[1]->getStartLoc(), Inst, Type))
240 return true;
241 if (popType(ErrorLoc, Type))
242 return true;
243 Stack.push_back(Type);
244 } else if (Name == "global.get") {
245 if (getGlobal(Operands[1]->getStartLoc(), Inst, Type))
246 return true;
247 Stack.push_back(Type);
248 } else if (Name == "global.set") {
249 if (getGlobal(Operands[1]->getStartLoc(), Inst, Type))
250 return true;
251 if (popType(ErrorLoc, Type))
252 return true;
253 } else if (Name == "table.get") {
254 if (getTable(Operands[1]->getStartLoc(), Inst, Type))
255 return true;
256 if (popType(ErrorLoc, wasm::ValType::I32))
257 return true;
258 Stack.push_back(Type);
259 } else if (Name == "table.set") {
260 if (getTable(Operands[1]->getStartLoc(), Inst, Type))
261 return true;
262 if (popType(ErrorLoc, Type))
263 return true;
264 if (popType(ErrorLoc, wasm::ValType::I32))
265 return true;
266 } else if (Name == "table.fill") {
267 if (getTable(Operands[1]->getStartLoc(), Inst, Type))
268 return true;
269 if (popType(ErrorLoc, wasm::ValType::I32))
270 return true;
271 if (popType(ErrorLoc, Type))
272 return true;
273 if (popType(ErrorLoc, wasm::ValType::I32))
274 return true;
275 } else if (Name == "drop") {
276 if (popType(ErrorLoc, {}))
277 return true;
278 } else if (Name == "end_block" || Name == "end_loop" || Name == "end_if" ||
279 Name == "else" || Name == "end_try") {
280 if (checkEnd(ErrorLoc, Name == "else"))
281 return true;
282 if (Name == "end_block")
283 Unreachable = false;
284 } else if (Name == "return") {
285 if (endOfFunction(ErrorLoc))
286 return true;
287 } else if (Name == "call_indirect" || Name == "return_call_indirect") {
288 // Function value.
289 if (popType(ErrorLoc, wasm::ValType::I32)) return true;
290 if (checkSig(ErrorLoc, LastSig)) return true;
291 if (Name == "return_call_indirect" && endOfFunction(ErrorLoc))
292 return true;
293 } else if (Name == "call" || Name == "return_call") {
294 const MCSymbolRefExpr *SymRef;
295 if (getSymRef(Operands[1]->getStartLoc(), Inst, SymRef))
296 return true;
297 auto WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
298 auto Sig = WasmSym->getSignature();
299 if (!Sig || WasmSym->getType() != wasm::WASM_SYMBOL_TYPE_FUNCTION)
300 return typeError(Operands[1]->getStartLoc(), StringRef("symbol ") +
301 WasmSym->getName() +
302 " missing .functype");
303 if (checkSig(ErrorLoc, *Sig)) return true;
304 if (Name == "return_call" && endOfFunction(ErrorLoc))
305 return true;
306 } else if (Name == "catch") {
307 const MCSymbolRefExpr *SymRef;
308 if (getSymRef(Operands[1]->getStartLoc(), Inst, SymRef))
309 return true;
310 const auto *WasmSym = cast<MCSymbolWasm>(&SymRef->getSymbol());
311 const auto *Sig = WasmSym->getSignature();
312 if (!Sig || WasmSym->getType() != wasm::WASM_SYMBOL_TYPE_TAG)
313 return typeError(Operands[1]->getStartLoc(), StringRef("symbol ") +
314 WasmSym->getName() +
315 " missing .tagtype");
316 // catch instruction pushes values whose types are specified in the tag's
317 // "params" part
318 Stack.insert(Stack.end(), Sig->Params.begin(), Sig->Params.end());
319 } else if (Name == "unreachable") {
320 Unreachable = true;
321 } else if (Name == "ref.is_null") {
322 if (popRefType(ErrorLoc))
323 return true;
324 Stack.push_back(wasm::ValType::I32);
325 } else {
326 // The current instruction is a stack instruction which doesn't have
327 // explicit operands that indicate push/pop types, so we get those from
328 // the register version of the same instruction.
329 auto RegOpc = WebAssembly::getRegisterOpcode(Opc);
330 assert(RegOpc != -1 && "Failed to get register version of MC instruction")(static_cast <bool> (RegOpc != -1 && "Failed to get register version of MC instruction"
) ? void (0) : __assert_fail ("RegOpc != -1 && \"Failed to get register version of MC instruction\""
, "llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp"
, 330, __extension__ __PRETTY_FUNCTION__))
;
331 const auto &II = MII.get(RegOpc);
332 // First pop all the uses off the stack and check them.
333 for (unsigned I = II.getNumOperands(); I > II.getNumDefs(); I--) {
334 const auto &Op = II.OpInfo[I - 1];
335 if (Op.OperandType == MCOI::OPERAND_REGISTER) {
336 auto VT = WebAssembly::regClassToValType(Op.RegClass);
337 if (popType(ErrorLoc, VT))
338 return true;
339 }
340 }
341 // Now push all the defs onto the stack.
342 for (unsigned I = 0; I < II.getNumDefs(); I++) {
343 const auto &Op = II.OpInfo[I];
344 assert(Op.OperandType == MCOI::OPERAND_REGISTER && "Register expected")(static_cast <bool> (Op.OperandType == MCOI::OPERAND_REGISTER
&& "Register expected") ? void (0) : __assert_fail (
"Op.OperandType == MCOI::OPERAND_REGISTER && \"Register expected\""
, "llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmTypeCheck.cpp"
, 344, __extension__ __PRETTY_FUNCTION__))
;
345 auto VT = WebAssembly::regClassToValType(Op.RegClass);
346 Stack.push_back(VT);
347 }
348 }
349 return false;
350}
351
352} // end namespace llvm

/build/source/llvm/include/llvm/ADT/Optional.h

1//===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file provides Optional, a template class modeled in the spirit of
11/// OCaml's 'opt' variant. The idea is to strongly type whether or not
12/// a value can be optional.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ADT_OPTIONAL_H
17#define LLVM_ADT_OPTIONAL_H
18
19#include "llvm/ADT/Hashing.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/STLForwardCompat.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/type_traits.h"
24#include <cassert>
25#include <new>
26#include <utility>
27
28namespace llvm {
29
30class raw_ostream;
31
32namespace optional_detail {
33
34/// Storage for any type.
35//
36// The specialization condition intentionally uses
37// llvm::is_trivially_{copy/move}_constructible instead of
38// std::is_trivially_{copy/move}_constructible. GCC versions prior to 7.4 may
39// instantiate the copy/move constructor of `T` when
40// std::is_trivially_{copy/move}_constructible is instantiated. This causes
41// compilation to fail if we query the trivially copy/move constructible
42// property of a class which is not copy/move constructible.
43//
44// The current implementation of OptionalStorage insists that in order to use
45// the trivial specialization, the value_type must be trivially copy
46// constructible and trivially copy assignable due to =default implementations
47// of the copy/move constructor/assignment. It does not follow that this is
48// necessarily the case std::is_trivially_copyable is true (hence the expanded
49// specialization condition).
50//
51// The move constructible / assignable conditions emulate the remaining behavior
52// of std::is_trivially_copyable.
53template <typename T,
54 bool = (llvm::is_trivially_copy_constructible<T>::value &&
55 std::is_trivially_copy_assignable<T>::value &&
56 (llvm::is_trivially_move_constructible<T>::value ||
57 !std::is_move_constructible<T>::value) &&
58 (std::is_trivially_move_assignable<T>::value ||
59 !std::is_move_assignable<T>::value))>
60class OptionalStorage {
61 union {
62 char empty;
63 T val;
64 };
65 bool hasVal = false;
66
67public:
68 ~OptionalStorage() { reset(); }
69
70 constexpr OptionalStorage() noexcept : empty() {}
71
72 constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
73 if (other.has_value()) {
74 emplace(other.val);
75 }
76 }
77 constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
78 if (other.has_value()) {
79 emplace(std::move(other.val));
80 }
81 }
82
83 template <class... Args>
84 constexpr explicit OptionalStorage(std::in_place_t, Args &&...args)
85 : val(std::forward<Args>(args)...), hasVal(true) {}
86
87 void reset() noexcept {
88 if (hasVal) {
89 val.~T();
90 hasVal = false;
91 }
92 }
93
94 constexpr bool has_value() const noexcept { return hasVal; }
95
96 T &value() &noexcept {
97 assert(hasVal)(static_cast <bool> (hasVal) ? void (0) : __assert_fail
("hasVal", "llvm/include/llvm/ADT/Optional.h", 97, __extension__
__PRETTY_FUNCTION__))
;
98 return val;
99 }
100 constexpr T const &value() const &noexcept {
101 assert(hasVal)(static_cast <bool> (hasVal) ? void (0) : __assert_fail
("hasVal", "llvm/include/llvm/ADT/Optional.h", 101, __extension__
__PRETTY_FUNCTION__))
;
102 return val;
103 }
104 T &&value() &&noexcept {
105 assert(hasVal)(static_cast <bool> (hasVal) ? void (0) : __assert_fail
("hasVal", "llvm/include/llvm/ADT/Optional.h", 105, __extension__
__PRETTY_FUNCTION__))
;
106 return std::move(val);
107 }
108
109 template <class... Args> void emplace(Args &&...args) {
110 reset();
111 ::new ((void *)std::addressof(val)) T(std::forward<Args>(args)...);
112 hasVal = true;
113 }
114
115 OptionalStorage &operator=(T const &y) {
116 if (has_value()) {
117 val = y;
118 } else {
119 ::new ((void *)std::addressof(val)) T(y);
120 hasVal = true;
121 }
122 return *this;
123 }
124 OptionalStorage &operator=(T &&y) {
125 if (has_value()) {
126 val = std::move(y);
127 } else {
128 ::new ((void *)std::addressof(val)) T(std::move(y));
129 hasVal = true;
130 }
131 return *this;
132 }
133
134 OptionalStorage &operator=(OptionalStorage const &other) {
135 if (other.has_value()) {
136 if (has_value()) {
137 val = other.val;
138 } else {
139 ::new ((void *)std::addressof(val)) T(other.val);
140 hasVal = true;
141 }
142 } else {
143 reset();
144 }
145 return *this;
146 }
147
148 OptionalStorage &operator=(OptionalStorage &&other) {
149 if (other.has_value()) {
150 if (has_value()) {
151 val = std::move(other.val);
152 } else {
153 ::new ((void *)std::addressof(val)) T(std::move(other.val));
154 hasVal = true;
155 }
156 } else {
157 reset();
158 }
159 return *this;
160 }
161};
162
163template <typename T> class OptionalStorage<T, true> {
164 union {
165 char empty;
166 T val;
167 };
168 bool hasVal = false;
169
170public:
171 ~OptionalStorage() = default;
172
173 constexpr OptionalStorage() noexcept : empty{} {}
174
175 constexpr OptionalStorage(OptionalStorage const &other) = default;
176 constexpr OptionalStorage(OptionalStorage &&other) = default;
177
178 OptionalStorage &operator=(OptionalStorage const &other) = default;
179 OptionalStorage &operator=(OptionalStorage &&other) = default;
180
181 template <class... Args>
182 constexpr explicit OptionalStorage(std::in_place_t, Args &&...args)
183 : val(std::forward<Args>(args)...), hasVal(true) {}
8
Assigned value is garbage or undefined
184
185 void reset() noexcept {
186 if (hasVal) {
187 val.~T();
188 hasVal = false;
189 }
190 }
191
192 constexpr bool has_value() const noexcept { return hasVal; }
193
194 T &value() &noexcept {
195 assert(hasVal)(static_cast <bool> (hasVal) ? void (0) : __assert_fail
("hasVal", "llvm/include/llvm/ADT/Optional.h", 195, __extension__
__PRETTY_FUNCTION__))
;
196 return val;
197 }
198 constexpr T const &value() const &noexcept {
199 assert(hasVal)(static_cast <bool> (hasVal) ? void (0) : __assert_fail
("hasVal", "llvm/include/llvm/ADT/Optional.h", 199, __extension__
__PRETTY_FUNCTION__))
;
200 return val;
201 }
202 T &&value() &&noexcept {
203 assert(hasVal)(static_cast <bool> (hasVal) ? void (0) : __assert_fail
("hasVal", "llvm/include/llvm/ADT/Optional.h", 203, __extension__
__PRETTY_FUNCTION__))
;
204 return std::move(val);
205 }
206
207 template <class... Args> void emplace(Args &&...args) {
208 reset();
209 ::new ((void *)std::addressof(val)) T(std::forward<Args>(args)...);
210 hasVal = true;
211 }
212
213 OptionalStorage &operator=(T const &y) {
214 if (has_value()) {
215 val = y;
216 } else {
217 ::new ((void *)std::addressof(val)) T(y);
218 hasVal = true;
219 }
220 return *this;
221 }
222 OptionalStorage &operator=(T &&y) {
223 if (has_value()) {
224 val = std::move(y);
225 } else {
226 ::new ((void *)std::addressof(val)) T(std::move(y));
227 hasVal = true;
228 }
229 return *this;
230 }
231};
232
233} // namespace optional_detail
234
235template <typename T> class Optional {
236 optional_detail::OptionalStorage<T> Storage;
237
238public:
239 using value_type = T;
240
241 constexpr Optional() = default;
242 constexpr Optional(std::nullopt_t) {}
243
244 constexpr Optional(const T &y) : Storage(std::in_place, y) {}
7
Calling constructor for 'OptionalStorage<llvm::wasm::ValType, true>'
245 constexpr Optional(const Optional &O) = default;
246
247 constexpr Optional(T &&y) : Storage(std::in_place, std::move(y)) {}
248 constexpr Optional(Optional &&O) = default;
249
250 template <typename... ArgTypes>
251 constexpr Optional(std::in_place_t, ArgTypes &&...Args)
252 : Storage(std::in_place, std::forward<ArgTypes>(Args)...) {}
253
254 Optional &operator=(T &&y) {
255 Storage = std::move(y);
256 return *this;
257 }
258 Optional &operator=(Optional &&O) = default;
259
260 /// Create a new object by constructing it in place with the given arguments.
261 template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
262 Storage.emplace(std::forward<ArgTypes>(Args)...);
263 }
264
265 static constexpr Optional create(const T *y) {
266 return y ? Optional(*y) : Optional();
267 }
268
269 Optional &operator=(const T &y) {
270 Storage = y;
271 return *this;
272 }
273 Optional &operator=(const Optional &O) = default;
274
275 void reset() { Storage.reset(); }
276
277 LLVM_DEPRECATED("Use &*X instead.", "&*X")__attribute__((deprecated("Use &*X instead.", "&*X"))
)
278 constexpr const T *getPointer() const { return &Storage.value(); }
279 LLVM_DEPRECATED("Use &*X instead.", "&*X")__attribute__((deprecated("Use &*X instead.", "&*X"))
)
280 T *getPointer() { return &Storage.value(); }
281 constexpr const T &value() const & { return Storage.value(); }
282 T &value() & { return Storage.value(); }
283
284 constexpr explicit operator bool() const { return has_value(); }
285 constexpr bool has_value() const { return Storage.has_value(); }
286 constexpr const T *operator->() const { return &Storage.value(); }
287 T *operator->() { return &Storage.value(); }
288 constexpr const T &operator*() const & { return value(); }
289 T &operator*() & { return value(); }
290
291 template <typename U> constexpr T value_or(U &&alt) const & {
292 return has_value() ? value() : std::forward<U>(alt);
293 }
294
295 /// Apply a function to the value if present; otherwise return None.
296 template <class Function>
297 auto transform(const Function &F) const & -> Optional<decltype(F(value()))> {
298 if (*this)
299 return F(value());
300 return std::nullopt;
301 }
302
303 T &&value() && { return std::move(Storage.value()); }
304 T &&operator*() && { return std::move(Storage.value()); }
305
306 template <typename U> T value_or(U &&alt) && {
307 return has_value() ? std::move(value()) : std::forward<U>(alt);
308 }
309
310 /// Apply a function to the value if present; otherwise return None.
311 template <class Function>
312 auto transform(
313 const Function &F) && -> Optional<decltype(F(std::move(*this).value()))> {
314 if (*this)
315 return F(std::move(*this).value());
316 return std::nullopt;
317 }
318};
319
320template<typename T>
321Optional(const T&) -> Optional<T>;
322
323template <class T> llvm::hash_code hash_value(const Optional<T> &O) {
324 return O ? hash_combine(true, *O) : hash_value(false);
325}
326
327template <typename T, typename U>
328constexpr bool operator==(const Optional<T> &X, const Optional<U> &Y) {
329 if (X && Y)
330 return *X == *Y;
331 return X.has_value() == Y.has_value();
332}
333
334template <typename T, typename U>
335constexpr bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
336 return !(X == Y);
337}
338
339template <typename T, typename U>
340constexpr bool operator<(const Optional<T> &X, const Optional<U> &Y) {
341 if (X && Y)
342 return *X < *Y;
343 return X.has_value() < Y.has_value();
344}
345
346template <typename T, typename U>
347constexpr bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
348 return !(Y < X);
349}
350
351template <typename T, typename U>
352constexpr bool operator>(const Optional<T> &X, const Optional<U> &Y) {
353 return Y < X;
354}
355
356template <typename T, typename U>
357constexpr bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
358 return !(X < Y);
359}
360
361template <typename T>
362constexpr bool operator==(const Optional<T> &X, std::nullopt_t) {
363 return !X;
364}
365
366template <typename T>
367constexpr bool operator==(std::nullopt_t, const Optional<T> &X) {
368 return X == std::nullopt;
369}
370
371template <typename T>
372constexpr bool operator!=(const Optional<T> &X, std::nullopt_t) {
373 return !(X == std::nullopt);
374}
375
376template <typename T>
377constexpr bool operator!=(std::nullopt_t, const Optional<T> &X) {
378 return X != std::nullopt;
379}
380
381template <typename T>
382constexpr bool operator<(const Optional<T> &, std::nullopt_t) {
383 return false;
384}
385
386template <typename T>
387constexpr bool operator<(std::nullopt_t, const Optional<T> &X) {
388 return X.has_value();
389}
390
391template <typename T>
392constexpr bool operator<=(const Optional<T> &X, std::nullopt_t) {
393 return !(std::nullopt < X);
394}
395
396template <typename T>
397constexpr bool operator<=(std::nullopt_t, const Optional<T> &X) {
398 return !(X < std::nullopt);
399}
400
401template <typename T>
402constexpr bool operator>(const Optional<T> &X, std::nullopt_t) {
403 return std::nullopt < X;
404}
405
406template <typename T>
407constexpr bool operator>(std::nullopt_t, const Optional<T> &X) {
408 return X < std::nullopt;
409}
410
411template <typename T>
412constexpr bool operator>=(const Optional<T> &X, std::nullopt_t) {
413 return std::nullopt <= X;
414}
415
416template <typename T>
417constexpr bool operator>=(std::nullopt_t, const Optional<T> &X) {
418 return X <= std::nullopt;
419}
420
421template <typename T>
422constexpr bool operator==(const Optional<T> &X, const T &Y) {
423 return X && *X == Y;
424}
425
426template <typename T>
427constexpr bool operator==(const T &X, const Optional<T> &Y) {
428 return Y && X == *Y;
429}
430
431template <typename T>
432constexpr bool operator!=(const Optional<T> &X, const T &Y) {
433 return !(X == Y);
434}
435
436template <typename T>
437constexpr bool operator!=(const T &X, const Optional<T> &Y) {
438 return !(X == Y);
439}
440
441template <typename T>
442constexpr bool operator<(const Optional<T> &X, const T &Y) {
443 return !X || *X < Y;
444}
445
446template <typename T>
447constexpr bool operator<(const T &X, const Optional<T> &Y) {
448 return Y && X < *Y;
449}
450
451template <typename T>
452constexpr bool operator<=(const Optional<T> &X, const T &Y) {
453 return !(Y < X);
454}
455
456template <typename T>
457constexpr bool operator<=(const T &X, const Optional<T> &Y) {
458 return !(Y < X);
459}
460
461template <typename T>
462constexpr bool operator>(const Optional<T> &X, const T &Y) {
463 return Y < X;
464}
465
466template <typename T>
467constexpr bool operator>(const T &X, const Optional<T> &Y) {
468 return Y < X;
469}
470
471template <typename T>
472constexpr bool operator>=(const Optional<T> &X, const T &Y) {
473 return !(X < Y);
474}
475
476template <typename T>
477constexpr bool operator>=(const T &X, const Optional<T> &Y) {
478 return !(X < Y);
479}
480
481raw_ostream &operator<<(raw_ostream &OS, std::nullopt_t);
482
483template <typename T, typename = decltype(std::declval<raw_ostream &>()
484 << std::declval<const T &>())>
485raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
486 if (O)
487 OS << *O;
488 else
489 OS << std::nullopt;
490 return OS;
491}
492
493} // end namespace llvm
494
495#endif // LLVM_ADT_OPTIONAL_H