summaryrefslogtreecommitdiff
path: root/source/slang/compiler.h
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-08-09 12:57:37 -0700
committerTim Foley <tfoley@nvidia.com>2017-08-10 13:05:04 -0700
commita5a436c4783fb75a0d089a6483219c06db91f593 (patch)
tree224c16ad374c5ed533a497beeb75753e7ce2d771 /source/slang/compiler.h
parent6e4830f4d74adef0a47c6503d84dc114240fafa3 (diff)
Make source location lightweight
Fixes #24 So far the code has used a representation for source locations that is heavy-weight, but typical of research or hobby compilers: a `struct` type containing a line number and a (heap-allocated) string. This is actually very convenient for debugging, but it means that any data structure that might contain a source location needs careful memory management (because of those strings) and has a tendency to bloat. The new represnetation is that a source location is just a pointer-sized integer. In the simplest mental model, you can think of this as just counting every byte of source text that is passed in, and using those to name locations. Finding the path and line number that corresponds to a location involves a lookup step, but we can arrange to store all the files in an array sorted by their start locations, and do a binary search. Finding line numbers inside a file is similarly fast (one you pay a one-time cost to build an array of starting offsets for lines). More advanced compilers like clang actually go further and create a unique range of source locations to represent a file each time it gets included, so that they can track the include stack and reproduce it in diagnostic messages. I'm not doing anything that clever here.
Diffstat (limited to 'source/slang/compiler.h')
-rw-r--r--source/slang/compiler.h45
1 files changed, 27 insertions, 18 deletions
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index fd40a62f6..5a6cb5c19 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -122,17 +122,7 @@ namespace Slang
// GLSL, // pass through GLSL to `glslang` library
};
- // Represents a single source file (either an on-disk file, or a
- // "virtual" file passed in as a string)
- class SourceFile : public RefObject
- {
- public:
- // The file path for a real file, or the nominal path for a virtual file
- String path;
-
- // The actual contents of the file
- String content;
- };
+ class SourceFile;
// A single translation unit requested to be compiled.
//
@@ -228,6 +218,10 @@ namespace Slang
// Are we being driven by the command-line `slangc`, and should act accordingly?
bool isCommandLineCompile = false;
+ // Source manager to help track files loaded
+ SourceManager sourceManagerStorage;
+ SourceManager* sourceManager;
+
// Output stuff
DiagnosticSink mSink;
String mDiagnosticOutput;
@@ -250,12 +244,9 @@ namespace Slang
Dictionary<String, RefPtr<ModuleDecl>> mapNameToLoadedModules;
- CompileRequest(Session* session)
- : mSession(session)
- {}
+ CompileRequest(Session* session);
- ~CompileRequest()
- {}
+ ~CompileRequest();
void parseTranslationUnit(
TranslationUnitRequest* translationUnit);
@@ -267,6 +258,10 @@ namespace Slang
int addTranslationUnit(SourceLanguage language, String const& name);
+ void addTranslationUnitSourceFile(
+ int translationUnitIndex,
+ SourceFile* sourceFile);
+
void addTranslationUnitSourceString(
int translationUnitIndex,
String const& path,
@@ -285,7 +280,7 @@ namespace Slang
String const& name,
String const& path,
String const& source,
- CodePosition const& loc);
+ SourceLoc const& loc);
void handlePoundImport(
String const& path,
@@ -293,7 +288,18 @@ namespace Slang
RefPtr<ModuleDecl> findOrImportModule(
String const& name,
- CodePosition const& loc);
+ SourceLoc const& loc);
+
+ SourceManager* getSourceManager()
+ {
+ return sourceManager;
+ }
+
+ void setSourceManager(SourceManager* sm)
+ {
+ sourceManager = sm;
+ mSink.sourceManager = sm;
+ }
};
void generateOutput(
@@ -324,6 +330,9 @@ namespace Slang
List<RefPtr<ModuleDecl>> loadedModuleCode;
+ SourceManager builtinSourceManager;
+
+ SourceManager* getBuiltinSourceManager() { return &builtinSourceManager; }
//