1
2
3
4
5
6 #include <cmajor/cmsxi/CompileUnit.hpp>
7 #include <cmajor/cmsxi/Context.hpp>
8 #include <fstream>
9
10 namespace cmsxi {
11
12 CompileUnit::CompileUnit(const std::string& filePath_) : filePath(filePath_), context(), cu(nullptr)
13 {
14 }
15
16 void CompileUnit::SetId(const std::string& id_)
17 {
18 id = id_;
19 }
20
21 void CompileUnit::SetSourceFilePath(const std::string& sourceFilePath_)
22 {
23 sourceFilePath = sourceFilePath_;
24 cu = context.GetMDStructRefForSourceFile(sourceFilePath);
25 }
26
27 Function* CompileUnit::GetOrInsertFunction(const std::string& name, FunctionType* type)
28 {
29 auto it = functionMap.find(name);
30 if (it != functionMap.cend())
31 {
32 return it->second;
33 }
34 else
35 {
36 Function* function = new Function(name, type, context);
37 functions.push_back(std::unique_ptr<Function>(function));
38 functionMap[name] = function;
39 return function;
40 }
41 }
42
43 void CompileUnit::Write()
44 {
45 std::ofstream file(filePath);
46 CodeFormatter formatter(file);
47 formatter.SetIndentSize(8);
48 formatter.Write("cu(");
49 formatter.Write(id);
50 formatter.Write(",");
51 cu->Write(formatter);
52 formatter.WriteLine(")");
53 formatter.WriteLine();
54 context.GetTypeRepository().Write(formatter);
55 context.GetDataRepository().Write(context, formatter);
56 bool first = true;
57 for (const auto& f : functions)
58 {
59 if (first)
60 {
61 first = false;
62 }
63 else
64 {
65 formatter.WriteLine();
66 }
67 f->Write(formatter, context);
68 }
69 context.GetMetadata().Write(formatter);
70 }
71
72 }