GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 3 / 0 / 3
Functions: -% 0 / 0 / 0
Branches: 46.7% 7 / 0 / 15

src/SourceFile.h
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <filesystem>
6 #include <string>
7
8 #include <exception/AntlrThrowingErrorListener.h>
9 #include <global/RuntimeModuleManager.h>
10 #include <util/CompilerWarning.h>
11 #include <util/GlobalDefinitions.h>
12
13 #include <llvm/IR/IRBuilder.h>
14 #include <llvm/Target/TargetMachine.h>
15
16 // Ignore some warnings in ANTLR-generated code
17 #pragma GCC diagnostic push
18 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
19 #include <SpiceLexer.h>
20 #include <SpiceParser.h>
21 #pragma GCC diagnostic pop
22
23 namespace spice::compiler {
24
25 // Forward declarations
26 class GlobalResourceManager;
27 class EntryNode;
28 class ASTNode;
29 class Timer;
30 struct CliOptions;
31 class SymbolTableEntry;
32 class Scope;
33 class Function;
34 class Type;
35
36 enum CompileStageType : uint8_t {
37 NONE,
38 LEXER,
39 PARSER,
40 CST_VISUALIZER,
41 AST_BUILDER,
42 AST_VISUALIZER,
43 IMPORT_COLLECTOR,
44 SYMBOL_TABLE_BUILDER,
45 TYPE_CHECKER_PRE,
46 TYPE_CHECKER_POST,
47 DEP_GRAPH_VISUALIZER,
48 IR_GENERATOR,
49 IR_OPTIMIZER,
50 OBJECT_EMITTER,
51 FINISHED
52 };
53
54 enum CompileStageIOType : uint8_t {
55 IO_CODE,
56 IO_TOKENS,
57 IO_CST,
58 IO_AST,
59 IO_IR,
60 IO_OBJECT_FILE,
61 };
62
63 struct SourceFileAntlrCtx {
64 // Create error handlers for lexer and parser
65 std::unique_ptr<AntlrThrowingErrorListener> lexerErrorHandler;
66 std::unique_ptr<AntlrThrowingErrorListener> parserErrorHandler;
67 std::unique_ptr<antlr4::ANTLRInputStream> inputStream;
68 std::unique_ptr<SpiceLexer> lexer;
69 std::unique_ptr<antlr4::CommonTokenStream> tokenStream;
70 std::unique_ptr<SpiceParser> parser;
71 };
72
73 struct TimerOutput {
74 uint64_t lexer = 0;
75 uint64_t parser = 0;
76 uint64_t cstVisualizer = 0;
77 uint64_t astBuilder = 0;
78 uint64_t astVisualizer = 0;
79 uint64_t importCollector = 0;
80 uint64_t symbolTableBuilder = 0;
81 uint64_t typeCheckerPre = 0;
82 uint64_t typeCheckerPost = 0;
83 uint64_t depGraphVisualizer = 0;
84 uint64_t irGenerator = 0;
85 uint64_t irOptimizer = 0;
86 uint64_t objectEmitter = 0;
87 };
88
89 struct CompilerOutput {
90 std::string cstString;
91 std::string astString;
92 std::string symbolTableString;
93 std::string depGraphString;
94 std::string irString;
95 std::string irOptString;
96 std::string asmString;
97 std::string typesString;
98 std::string cacheStats;
99 std::vector<CompilerWarning> warnings;
100 TimerOutput times;
101 };
102
103 struct NameRegistryEntry {
104 std::string name;
105 uint64_t typeId; // Set for structs, interfaces, enums and aliases
106 SymbolTableEntry *targetEntry;
107 Scope *targetScope;
108 SymbolTableEntry *importEntry = nullptr;
109 };
110
111 class SourceFile {
112 public:
113 // Constructors
114 SourceFile(GlobalResourceManager &resourceManager, SourceFile *parent, std::string name, const std::filesystem::path &filePath,
115 bool stdFile);
116
117 // Prevent copy
118 SourceFile(const SourceFile &) = delete;
119 SourceFile &operator=(const SourceFile &) = delete;
120
121 // Friend classes
122 friend class RuntimeModuleManager;
123
124 // Compiler pipeline triggers
125 void runLexer();
126 void runParser();
127 void runCSTVisualizer();
128 void runASTBuilder();
129 void runASTVisualizer();
130 void runImportCollector();
131 void runSymbolTableBuilder();
132
133 private:
134 void runTypeCheckerPre();
135 void runTypeCheckerPost();
136
137 public:
138 void runDependencyGraphVisualizer();
139 void runIRGenerator();
140 void runDefaultIROptimizer();
141 void runPreLinkIROptimizer();
142 void runBitcodeLinker();
143 void runPostLinkIROptimizer();
144 void runObjectEmitter();
145 void concludeCompilation();
146
147 // Shortcuts
148 void runFrontEnd();
149 void runMiddleEnd();
150 void runBackEnd();
151
152 // Public methods
153 void addDependency(SourceFile *sourceFile, const ASTNode *declNode, const std::string &dependencyName, const std::string &path);
154 [[nodiscard]] bool imports(const SourceFile *sourceFile) const;
155 [[nodiscard]] bool isAlreadyImported(const std::string &filePathSearch, std::stack<const SourceFile *> &circle) const;
156 SourceFile *requestRuntimeModule(RuntimeModule runtimeModule);
157 bool isRuntimeModuleAvailable(RuntimeModule runtimeModule) const;
158 void addNameRegistryEntry(const std::string &symbolName, uint64_t typeId, SymbolTableEntry *entry, Scope *scope,
159 bool keepNewOnCollision = true, SymbolTableEntry *importEntry = nullptr);
160 [[nodiscard]] const NameRegistryEntry *getNameRegistryEntry(const std::string &symbolName) const;
161 [[nodiscard]] llvm::Type *getLLVMType(const Type *type);
162 void checkForSoftErrors() const;
163 void collectAndPrintWarnings();
164 const SourceFile *getRootSourceFile() const;
165 bool isRT(RuntimeModule runtimeModule) const;
166
3/7
✓ Branch 20 → 21 taken 1003 times.
✗ Branch 20 → 58 not taken.
✓ Branch 31 → 32 taken 188 times.
✗ Branch 31 → 61 not taken.
✗ Branch 31 → 73 not taken.
✓ Branch 64 → 65 taken 63 times.
✗ Branch 64 → 149 not taken.
1331 ALWAYS_INLINE bool isStringRT() const { return isRT(STRING_RT); }
167
1/2
✓ Branch 16 → 17 taken 1167 times.
✗ Branch 16 → 58 not taken.
1167 ALWAYS_INLINE bool isMemoryRT() const { return isRT(MEMORY_RT); }
168
3/6
✓ Branch 21 → 22 taken 396 times.
✗ Branch 21 → 111 not taken.
✓ Branch 79 → 80 taken 105 times.
✗ Branch 79 → 120 not taken.
✓ Branch 176 → 177 taken 206 times.
✗ Branch 176 → 284 not taken.
707 ALWAYS_INLINE bool isRttiRT() const { return isRT(RTTI_RT); }
169
170 // Public fields
171 std::string name;
172 std::string fileName;
173 std::filesystem::path filePath;
174 std::string fileDir;
175 bool isStdFile = false;
176 bool isMainFile = true;
177 bool alwaysKeepSymbolsOnNameCollision = false;
178 bool ignoreWarnings = false;
179 bool restoredFromCache = false;
180 bool reVisitRequested = true;
181 CompileStageType previousStage = NONE;
182 SourceFileAntlrCtx antlrCtx;
183 CompilerOutput compilerOutput;
184 SourceFile *parent;
185 std::string cacheKey;
186 EntryNode *ast = nullptr;
187 std::unique_ptr<Scope> globalScope;
188 llvm::LLVMContext context;
189 llvm::IRBuilder<> builder;
190 std::unique_ptr<llvm::TargetMachine> targetMachine;
191 std::unique_ptr<llvm::Module> llvmModule;
192 std::map<std::string, SourceFile *> dependencies; // Has to be an ordered map to keep the compilation order deterministic
193 std::vector<const SourceFile *> dependants;
194 std::map<std::string, NameRegistryEntry> exportedNameRegistry;
195 std::vector<const Function *> testFunctions;
196
197 private:
198 // Private fields
199 GlobalResourceManager &resourceManager;
200 const CliOptions &cliOptions;
201 std::unordered_map<const Type *, llvm::Type *> typeToLLVMTypeMapping;
202 uint8_t importedRuntimeModules = 0;
203 uint8_t totalTypeCheckerRuns = 0;
204
205 // Private methods
206 bool haveAllDependantsBeenTypeChecked() const;
207 void mergeNameRegistries(const SourceFile &importedSourceFile, const std::string &importName);
208 void dumpCacheStats();
209 void dumpCompilationStats() const;
210 void dumpOutput(const std::string &content, const std::string &caption, const std::string &fileSuffix) const;
211 void visualizerPreamble(std::stringstream &output) const;
212 void visualizerOutput(std::string outputName, const std::string &output) const;
213 void printStatusMessage(const char *stage, const CompileStageIOType &in, const CompileStageIOType &out, uint64_t stageRuntime,
214 unsigned short stageRuns = 0) const;
215 };
216
217 } // namespace spice::compiler
218