1
2
3
4
5
6 #ifndef CMAJOR_CMSXI_DATA_INCLUDED
7 #define CMAJOR_CMSXI_DATA_INCLUDED
8 #include <cmajor/cmsxi/Type.hpp>
9
10 namespace cmsxi {
11
12 class GlobalVariable : public Value
13 {
14 public:
15 GlobalVariable(Type* type_, const std::string& name_);
16 std::string Name(Context& context) override;
17 Type* GetType(Context& context) override;
18 void SetInitializer(ConstantValue* initializer_) { initializer = initializer_; }
19 ConstantValue* Initializer() const { return initializer; }
20 void Write(Context& context, CodeFormatter& formatter);
21 void SetLinkOnce() { linkOnce = true; }
22 private:
23 Type* type;
24 std::string name;
25 ConstantValue* initializer;
26 bool linkOnce;
27 };
28
29 class DataRepository
30 {
31 public:
32 DataRepository();
33 DataRepository(const DataRepository&) = delete;
34 DataRepository& operator=(const DataRepository&) = delete;
35 GlobalVariable* GetOrInsertGlobal(const std::string& name, Type* type);
36 GlobalVariable* CreateGlobalStringPtr(Context& context, const std::string& stringValue);
37 void Write(Context& context, CodeFormatter& formatter);
38 void SetCompileUnitId(const std::string& compileUnitId_);
39 private:
40 std::vector<std::std::unique_ptr<GlobalVariable>>globalVariableDefinitions;
41 std::unordered_map<std::string, GlobalVariable*> globalVariableMap;
42 int nextStringId;
43 std::string compileUnitId;
44 };
45
46 }
47
48 #endif // CMAJOR_CMSXI_DATA_INCLUDED