xref: /llvm-project/flang/include/flang/Evaluate/static-data.h (revision 23c2bedfd93cfacc62009425c464e659a34e92e6)
1 //===-- include/flang/Evaluate/static-data.h --------------------*- 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 #ifndef FORTRAN_EVALUATE_STATIC_DATA_H_
10 #define FORTRAN_EVALUATE_STATIC_DATA_H_
11 
12 // Represents constant static data objects
13 
14 #include "formatting.h"
15 #include "type.h"
16 #include "flang/Common/idioms.h"
17 #include <cinttypes>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 namespace llvm {
24 class raw_ostream;
25 }
26 
27 namespace Fortran::evaluate {
28 
29 class StaticDataObject {
30 public:
31   using Pointer = std::shared_ptr<StaticDataObject>;
32 
33   StaticDataObject(const StaticDataObject &) = delete;
34   StaticDataObject(StaticDataObject &&) = delete;
35   StaticDataObject &operator=(const StaticDataObject &) = delete;
36   StaticDataObject &operator=(StaticDataObject &&) = delete;
37 
Create()38   static Pointer Create() { return Pointer{new StaticDataObject}; }
39 
name()40   const std::string &name() const { return name_; }
set_name(std::string n)41   StaticDataObject &set_name(std::string n) {
42     name_ = n;
43     return *this;
44   }
45 
alignment()46   int alignment() const { return alignment_; }
set_alignment(int a)47   StaticDataObject &set_alignment(int a) {
48     CHECK(a >= 0);
49     alignment_ = a;
50     return *this;
51   }
52 
itemBytes()53   int itemBytes() const { return itemBytes_; }
set_itemBytes(int b)54   StaticDataObject &set_itemBytes(int b) {
55     CHECK(b >= 1);
56     itemBytes_ = b;
57     return *this;
58   }
59 
data()60   const std::vector<std::uint8_t> &data() const { return data_; }
data()61   std::vector<std::uint8_t> &data() { return data_; }
62 
63   StaticDataObject &Push(const std::string &, bool /*ignored*/ = false);
64   StaticDataObject &Push(const std::u16string &, bool bigEndian = false);
65   StaticDataObject &Push(const std::u32string &, bool bigEndian = false);
66   std::optional<std::string> AsString() const;
67   std::optional<std::u16string> AsU16String(bool bigEndian = false) const;
68   std::optional<std::u32string> AsU32String(bool bigEndian = false) const;
69   llvm::raw_ostream &AsFortran(
70       llvm::raw_ostream &, bool bigEndian = false) const;
71 
72 private:
StaticDataObject()73   StaticDataObject() {}
74 
75   std::string name_;
76   int alignment_{1};
77   int itemBytes_{1};
78   std::vector<std::uint8_t> data_;
79 };
80 } // namespace Fortran::evaluate
81 #endif // FORTRAN_EVALUATE_STATIC_DATA_H_
82