1 //===-- lib/Evaluate/static-data.cpp --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "flang/Evaluate/static-data.h"
10 #include "flang/Parser/characters.h"
12 namespace Fortran::evaluate
{
14 llvm::raw_ostream
&StaticDataObject::AsFortran(
15 llvm::raw_ostream
&o
, bool bigEndian
) const {
16 if (auto string
{AsString()}) {
17 o
<< parser::QuoteCharacterLiteral(*string
);
18 } else if (auto string
{AsU16String(bigEndian
)}) {
19 o
<< "2_" << parser::QuoteCharacterLiteral(*string
);
20 } else if (auto string
{AsU32String(bigEndian
)}) {
21 o
<< "4_" << parser::QuoteCharacterLiteral(*string
);
28 StaticDataObject
&StaticDataObject::Push(const std::string
&string
, bool) {
29 for (auto ch
: string
) {
30 data_
.push_back(static_cast<std::uint8_t>(ch
));
35 StaticDataObject
&StaticDataObject::Push(
36 const std::u16string
&string
, bool bigEndian
) {
37 int shift
{bigEndian
? 8 : 0};
38 for (auto ch
: string
) {
39 data_
.push_back(static_cast<std::uint8_t>(ch
>> shift
));
40 data_
.push_back(static_cast<std::uint8_t>(ch
>> (shift
^ 8)));
45 StaticDataObject
&StaticDataObject::Push(
46 const std::u32string
&string
, bool bigEndian
) {
47 int shift
{bigEndian
? 24 : 0};
48 for (auto ch
: string
) {
49 data_
.push_back(static_cast<std::uint8_t>(ch
>> shift
));
50 data_
.push_back(static_cast<std::uint8_t>(ch
>> (shift
^ 8)));
51 data_
.push_back(static_cast<std::uint8_t>(ch
>> (shift
^ 16)));
52 data_
.push_back(static_cast<std::uint8_t>(ch
>> (shift
^ 24)));
57 std::optional
<std::string
> StaticDataObject::AsString() const {
58 if (itemBytes_
<= 1) {
60 for (std::uint8_t byte
: data_
) {
61 result
+= static_cast<char>(byte
);
63 return {std::move(result
)};
68 std::optional
<std::u16string
> StaticDataObject::AsU16String(
69 bool bigEndian
) const {
70 if (itemBytes_
== 2) {
71 int shift
{bigEndian
? 8 : 0};
72 std::u16string result
;
73 auto end
{data_
.cend()};
74 for (auto byte
{data_
.cbegin()}; byte
< end
;) {
75 result
+= static_cast<char16_t
>(*byte
++) << shift
|
76 static_cast<char16_t
>(*byte
++) << (shift
^ 8);
78 return {std::move(result
)};
83 std::optional
<std::u32string
> StaticDataObject::AsU32String(
84 bool bigEndian
) const {
85 if (itemBytes_
== 4) {
86 int shift
{bigEndian
? 24 : 0};
87 std::u32string result
;
88 auto end
{data_
.cend()};
89 for (auto byte
{data_
.cbegin()}; byte
< end
;) {
90 result
+= static_cast<char32_t
>(*byte
++) << shift
|
91 static_cast<char32_t
>(*byte
++) << (shift
^ 8) |
92 static_cast<char32_t
>(*byte
++) << (shift
^ 16) |
93 static_cast<char32_t
>(*byte
++) << (shift
^ 24);
95 return {std::move(result
)};
99 } // namespace Fortran::evaluate