1
2
3
4
5
6 #ifndef CMAJOR_SYMBOLS_VARIABLE_SYMBOL_INCLUDED
7 #define CMAJOR_SYMBOLS_VARIABLE_SYMBOL_INCLUDED
8 #include <cmajor/symbols/Value.hpp>
9 #include <cmajor/symbols/TypeMap.hpp>
10 #include <cmajor/ir/Emitter.hpp>
11
12 namespace cmajor { namespace symbols {
13
14 using namespace cmajor::ir;
15
16 class VariableSymbol : public Symbol
17 {
18 public:
19 VariableSymbol(SymbolType symbolType_, const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& name_);
20 bool IsVariableSymbol() const override { return true; }
21 void Write(SymbolWriter& writer) override;
22 void Read(SymbolReader& reader) override;
23 void EmplaceType(TypeSymbol* typeSymbol, int index) override;
24 const TypeSymbol* GetType() const { return type; }
25 TypeSymbol* GetType() { return type; }
26 void SetType(TypeSymbol* typeSymbol) { type = typeSymbol; }
27 const ContainerScope* GetTypeScope() const override;
28 ContainerScope* GetTypeScope() override;
29 std::u32string Info() const override { return Name(); }
30 const char* ClassName() const override { return "VariableSymbol"; }
31 void Check() override;
32 std::string GetSymbolHelp() const override;
33 private:
34 TypeSymbol* type;
35 };
36
37 class ParameterSymbol : public VariableSymbol
38 {
39 public:
40 ParameterSymbol(const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& name_);
41 void Write(SymbolWriter& writer) override;
42 void Read(SymbolReader& reader) override;
43 bool IsExportSymbol() const override;
44 SymbolAccess DeclaredAccess() const override { return SymbolAccess::public_; }
45 std::string TypeString() const override { return "parameter"; }
46 std::unique_ptr<sngxml::dom::Element> CreateDomElement(TypeMap& typeMap) override;
47 const char* ClassName() const override { return "ParameterSymbol"; }
48 bool ArtificialName() const { return artificialName; }
49 void SetArtificialName() { artificialName = true; }
50 std::u32string CodeName() const override;
51 ParameterSymbol* Clone() const;
52 std::string GetSymbolCategoryStr() const override { return "PA"; }
53 std::string GetSymbolCategoryDescription() const override { return "parameter"; }
54 std::string GetSymbolHelp() const override;
55 private:
56 bool artificialName;
57 };
58
59 class LocalVariableSymbol : public VariableSymbol
60 {
61 public:
62 LocalVariableSymbol(const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& name_);
63 SymbolAccess DeclaredAccess() const override { return SymbolAccess::public_; }
64 bool IsExportSymbol() const override { return false; }
65 std::unique_ptr<sngxml::dom::Element> CreateDomElement(TypeMap& typeMap) override;
66 const char* ClassName() const override { return "LocalVariableSymbol"; }
67 std::string GetSymbolCategoryStr() const override { return "LV"; }
68 std::string GetSymbolCategoryDescription() const override { return "local variable"; }
69 std::string GetSymbolHelp() const override;
70 };
71
72 class MemberVariableSymbol : public VariableSymbol
73 {
74 public:
75 MemberVariableSymbol(const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& name_);
76 void Write(SymbolWriter& writer) override;
77 void Read(SymbolReader& reader) override;
78 bool IsExportSymbol() const override;
79 void Accept(SymbolCollector* collector) override;
80 void Dump(CodeFormatter& formatter) override;
81 std::string TypeString() const override { return "member_variable"; }
82 std::string Syntax() const override;
83 void SetSpecifiers(Specifiers specifiers);
84 int32_t LayoutIndex() const { return layoutIndex; }
85 void SetLayoutIndex(int32_t layoutIndex_) { layoutIndex = layoutIndex_; }
86 void* GetDIMemberType(Emitter& emitter, uint64_t offsetInBits);
87 std::unique_ptr<sngxml::dom::Element> CreateDomElement(TypeMap& typeMap) override;
88 const char* ClassName() const override { return "MemberVariableSymbol"; }
89 void Check() override;
90 std::string GetSymbolCategoryStr() const override { return "MV"; }
91 std::string GetSymbolCategoryDescription() const override { return "member variable"; }
92 private:
93 int32_t layoutIndex;
94 };
95
96 class GlobalVariableSymbol;
97
98 class GlobalVariableGroupSymbol : public Symbol
99 {
100 public:
101 GlobalVariableGroupSymbol(const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& name_);
102 bool IsExportSymbol() const override { return false; }
103 std::string TypeString() const override { return "global_variable_group"; }
104 void ComputeMangledName() override;
105 void AddGlobalVariable(GlobalVariableSymbol* globalVariableSymbol);
106 void RemoveGlobalVariable(GlobalVariableSymbol* globalVariableSymbol);
107 bool IsEmpty() const;
108 std::u32string Info() const override { return Name(); }
109 const char* ClassName() const override { return "GlobalVariableGroupSymbol"; }
110 void CollectGlobalVariables(const std::string& compileUnitFilePath, std::std::vector<GlobalVariableSymbol*>&globalVariables) const;
111 std::string GetSymbolCategoryStr() const override { return "GV"; }
112 std::string GetSymbolCategoryDescription() const override { return "global variable"; }
113 const ContainerScope* GetTypeScope() const override;
114 ContainerScope* GetTypeScope() override;
115 std::string GetSymbolHelp() const override;
116 private:
117 std::vector<std::std::pair<GlobalVariableSymbol*, std::string>>globalVariableSymbols;
118 };
119
120 class GlobalVariableSymbol : public VariableSymbol
121 {
122 public:
123 GlobalVariableSymbol(const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& groupName_, const std::string& compileUnitId, const std::string& compileUnitFilePath_);
124 GlobalVariableSymbol(const Span& span_, const boost::uuids::uuid& sourceModuleId_, const std::u32string& name_);
125 void Write(SymbolWriter& writer) override;
126 void Read(SymbolReader& reader) override;
127 bool IsExportSymbol() const override;
128 bool IsGlobalVariableSymbol() const override { return true; }
129 void Accept(SymbolCollector* collector) override;
130 void Dump(CodeFormatter& formatter) override;
131 std::string TypeString() const override { return "global_variable"; }
132 void ComputeMangledName() override;
133 std::string Syntax() const override;
134 void SetSpecifiers(Specifiers specifiers);
135 const char* ClassName() const override { return "GlobalVariableSymbol"; }
136 void SetInitializer(std::std::unique_ptr<Value>&&initializer_);
137 Value* Initializer() const { return initializer.get(); }
138 void* IrObject(Emitter& emitter) override;
139 void CreateIrObject(Emitter& emitter);
140 const std::u32string& GroupName() const { return groupName; }
141 const std::string& CompileUnitFilePath() const { return compileUnitFilePath; }
142 void SetGlobalVariableGroup(GlobalVariableGroupSymbol* globalVariableGroup_) { globalVariableGroup = globalVariableGroup_; }
143 std::unique_ptr<Symbol> RemoveFromParent() override;
144 std::string GetSymbolCategoryStr() const override { return "GV"; }
145 std::string GetSymbolCategoryDescription() const override { return "global variable"; }
146 private:
147 std::u32string groupName;
148 std::string compileUnitFilePath;
149 std::unique_ptr<Value> initializer;
150 GlobalVariableGroupSymbol* globalVariableGroup;
151 };
152
153 } }
154
155 #endif // CMAJOR_SYMBOLS_VARIABLE_SYMBOL_INCLUDED