GCC Code Coverage Report


Directory: ../
File: src/SourceFile.h
Date: 2025-03-05 01:50:32
Exec Total Coverage
Lines: 3 3 100.0%
Functions: 0 0 -%
Branches: 5 10 50.0%

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