static-data.cpp
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//===-- lib/Evaluate/static-data.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Evaluate/static-data.h"
#include "flang/Parser/characters.h"
namespace Fortran::evaluate {
bool StaticDataObject::bigEndian{false};
llvm::raw_ostream &StaticDataObject::AsFortran(llvm::raw_ostream &o) const {
if (auto string{AsString()}) {
o << parser::QuoteCharacterLiteral(*string);
} else if (auto string{AsU16String()}) {
o << "2_" << parser::QuoteCharacterLiteral(*string);
} else if (auto string{AsU32String()}) {
o << "4_" << parser::QuoteCharacterLiteral(*string);
} else {
CRASH_NO_CASE;
}
return o;
}
StaticDataObject &StaticDataObject::Push(const std::string &string) {
for (auto ch : string) {
data_.push_back(static_cast<std::uint8_t>(ch));
}
return *this;
}
StaticDataObject &StaticDataObject::Push(const std::u16string &string) {
int shift{bigEndian * 8};
for (auto ch : string) {
data_.push_back(static_cast<std::uint8_t>(ch >> shift));
data_.push_back(static_cast<std::uint8_t>(ch >> (shift ^ 8)));
}
return *this;
}
StaticDataObject &StaticDataObject::Push(const std::u32string &string) {
int shift{bigEndian * 24};
for (auto ch : string) {
data_.push_back(static_cast<std::uint8_t>(ch >> shift));
data_.push_back(static_cast<std::uint8_t>(ch >> (shift ^ 8)));
data_.push_back(static_cast<std::uint8_t>(ch >> (shift ^ 16)));
data_.push_back(static_cast<std::uint8_t>(ch >> (shift ^ 24)));
}
return *this;
}
std::optional<std::string> StaticDataObject::AsString() const {
if (itemBytes_ <= 1) {
std::string result;
for (std::uint8_t byte : data_) {
result += static_cast<char>(byte);
}
return {std::move(result)};
}
return std::nullopt;
}
std::optional<std::u16string> StaticDataObject::AsU16String() const {
if (itemBytes_ == 2) {
int shift{bigEndian * 8};
std::u16string result;
auto end{data_.cend()};
for (auto byte{data_.cbegin()}; byte < end;) {
result += static_cast<char16_t>(*byte++) << shift |
static_cast<char16_t>(*byte++) << (shift ^ 8);
}
return {std::move(result)};
}
return std::nullopt;
}
std::optional<std::u32string> StaticDataObject::AsU32String() const {
if (itemBytes_ == 4) {
int shift{bigEndian * 24};
std::u32string result;
auto end{data_.cend()};
for (auto byte{data_.cbegin()}; byte < end;) {
result += static_cast<char32_t>(*byte++) << shift |
static_cast<char32_t>(*byte++) << (shift ^ 8) |
static_cast<char32_t>(*byte++) << (shift ^ 16) |
static_cast<char32_t>(*byte++) << (shift ^ 24);
}
return {std::move(result)};
}
return std::nullopt;
}
} // namespace Fortran::evaluate