summaryrefslogtreecommitdiff
path: root/source/slang/slang-preprocessor.h
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-09-24 13:09:40 -0700
committerGitHub <noreply@github.com>2020-09-24 13:09:40 -0700
commit150218bec9e992d32833dd9a0c1396a4d7c12b7e (patch)
treec544eba21b13217619b9c48546c6ffbbbe0ec34b /source/slang/slang-preprocessor.h
parentfd2ac531c0fcb05bba421184b7443dc034281322 (diff)
Refactor preprocessor API to avoid coupling (#1559)
Based on review feedback from #1556, this change updates the Slang preprocessor so that it is no longer coupled to policy details from higher levels of the software stack. In particular, the preprocessor used to: * Deal with updating the list of file paths that a `Module` depends on. * (As of #1556) detect NVAPI-related macro definitions and use them to construct an AST-level `Modifier` attached to the `ModuleDecl`. This change introduces a callback interface where the `Preprocessor` calls out to a `PreprocessorHandler` at certain points during execution, allowing the handler to introduce custom logic that suits a particular high-level use case. This change also removes the dependence of the preprocessor on the `Linkage`, because in practice only a small number of its sub-objects were needed. As a convenience, a wrapper function that takes a `Linkage` was left in place so that the existing call sites didn't have to change very much.
Diffstat (limited to 'source/slang/slang-preprocessor.h')
-rw-r--r--source/slang/slang-preprocessor.h73
1 files changed, 62 insertions, 11 deletions
diff --git a/source/slang/slang-preprocessor.h b/source/slang/slang-preprocessor.h
index 7c72a859c..efecb912b 100644
--- a/source/slang/slang-preprocessor.h
+++ b/source/slang/slang-preprocessor.h
@@ -10,21 +10,72 @@
namespace Slang {
-class ASTBuilder;
class DiagnosticSink;
class Linkage;
-class Module;
-class ModuleDecl;
-// Take a string of source code and preprocess it into a list of tokens.
+struct Preprocessor;
+
+ /// A handler for callbacks invoked by the preprocessor.
+ ///
+ /// A client of the preprocessor can implement its own `PreprocessorHandler` subtype
+ /// in order to insert custom logic that implements higher-level policies
+ /// that the preprocessor shouldn't need to understand.
+ ///
+struct PreprocessorHandler
+{
+ virtual void handleEndOfFile(Preprocessor* preprocessor);
+ virtual void handleFileDependency(String const& path);
+};
+
+ /// Description of a preprocessor options/dependencies
+struct PreprocessorDesc
+{
+ /// Required: sink to use when emitting preprocessor diagnostic messages
+ DiagnosticSink* sink = nullptr;
+
+ /// Required: name pool to use when creating `Name`s from strings
+ NamePool* namePool = nullptr;
+
+ /// Required: file system to use when looking up files
+ ISlangFileSystemExt* fileSystem = nullptr;
+
+ /// Required: source manager to use when loading source files
+ SourceManager* sourceManager = nullptr;
+
+ /// Optional: include system to use when resolving `#include` directives
+ IncludeSystem* includeSystem = nullptr;
+
+ /// Optional: preprocessor `#define`s to assume are set on input
+ Dictionary<String, String> const* defines = nullptr;
+
+ /// Optional: handler for callbacks invoked during preprocessing
+ PreprocessorHandler* handler = nullptr;
+};
+
+ /// Take a source `file` and preprocess it into a list of tokens.
+TokenList preprocessSource(
+ SourceFile* file,
+ PreprocessorDesc const& desc);
+
+ /// Convenience wrapper for `preprocessSource` when a `Linkage` is available
TokenList preprocessSource(
- SourceFile* file,
- DiagnosticSink* sink,
- IncludeSystem* includeSystem,
- Dictionary<String, String> defines,
- Linkage* linkage,
- Module* parentModule,
- ASTBuilder* astBuilder = nullptr);
+ SourceFile* file,
+ DiagnosticSink* sink,
+ IncludeSystem* includeSystem,
+ Dictionary<String, String> const& defines,
+ Linkage* linkage,
+ PreprocessorHandler* handler = nullptr);
+
+// The following functions are intended to be used inside of implementations
+// of the `PreprocessorHandler` interface, in order to query the current
+// state of the preprocessor.
+
+ /// Try to look up a macro with the given `macroName` and produce its value as a string
+Result findMacroValue(
+ Preprocessor* preprocessor,
+ char const* macroName,
+ String& outValue,
+ SourceLoc& outLoc);
} // namespace Slang