diff options
Diffstat (limited to 'source/slang/slang-compiler.cpp')
| -rwxr-xr-x | source/slang/slang-compiler.cpp | 109 |
1 files changed, 32 insertions, 77 deletions
diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp index c643f825a..72cf2dfbe 100755 --- a/source/slang/slang-compiler.cpp +++ b/source/slang/slang-compiler.cpp @@ -8,7 +8,6 @@ #include "../core/slang-riff.h" #include "../core/slang-type-text-util.h" - #include "slang-check.h" #include "slang-compiler.h" #include "slang-lexer.h" @@ -841,83 +840,39 @@ namespace Slang return UnownedStringSlice(); } - /// Read a file in the context of handling a preprocessor directive - static SlangResult readFile( - Linkage* linkage, - String const& path, - ISlangBlob** outBlob) - { - // The actual file loading will be handled by the file system - // associated with the parent linkage. - // - auto fileSystemExt = linkage->getFileSystemExt(); - SLANG_RETURN_ON_FAIL(fileSystemExt->loadFile(path.getBuffer(), outBlob)); - - return SLANG_OK; - } - struct FxcIncludeHandler : ID3DInclude { - Linkage* linkage; - DiagnosticSink* sink; - IncludeHandler* includeHandler; - PathInfo rootPathInfo; - - STDMETHOD(Open)(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes) override + + STDMETHOD(Open)(D3D_INCLUDE_TYPE includeType, LPCSTR fileName, LPCVOID parentData, LPCVOID* outData, UINT* outSize) override { - SLANG_UNUSED(IncludeType); - SLANG_UNUSED(pParentData); - - String path(pFileName); + SLANG_UNUSED(includeType); + // NOTE! The pParentData means the *text* of any previous include. + // In order to work out what *path* that came from, we need to seach which source file it came from, and + // use it's path - SourceLoc loc; + // Assume the root pathInfo initially + PathInfo includedFromPathInfo = m_rootPathInfo; - PathInfo includedFromPathInfo = rootPathInfo; - - if (!includeHandler) + // Lets try and find the parent source if there is any + if (parentData) { - return SLANG_E_NOT_IMPLEMENTED; - } - - // Find the path relative to the foundPath - PathInfo filePathInfo; - if (SLANG_FAILED(includeHandler->findFile(path, includedFromPathInfo.foundPath, filePathInfo))) - { - return SLANG_E_CANNOT_OPEN; - } - - // We must have a uniqueIdentity to be compare - if (!filePathInfo.hasUniqueIdentity()) - { - return SLANG_E_ABORT; - } - - // Simplify the path - filePathInfo.foundPath = includeHandler->simplifyPath(filePathInfo.foundPath); - - // See if this an already loaded source file - auto sourceManager = linkage->getSourceManager(); - SourceFile* sourceFile = sourceManager->findSourceFileRecursively(filePathInfo.uniqueIdentity); - - // If not create a new one, and add to the list of known source files - if (!sourceFile) - { - ComPtr<ISlangBlob> foundSourceBlob; - if (SLANG_FAILED(readFile(linkage, filePathInfo.foundPath, foundSourceBlob.writeRef()))) + SourceFile* foundSourceFile = m_system.getSourceManager()->findSourceFileByContentRecursively((const char*)parentData); + if (foundSourceFile) { - return SLANG_E_CANNOT_OPEN; + includedFromPathInfo = foundSourceFile->getPathInfo(); } - - sourceFile = sourceManager->createSourceFileWithBlob(filePathInfo, foundSourceBlob); - sourceManager->addSourceFile(filePathInfo.uniqueIdentity, sourceFile); } - // This is a new parse (even if it's a pre-existing source file), so create a new SourceUnit - SourceView* sourceView = sourceManager->createSourceView(sourceFile, &filePathInfo); + String path(fileName); + PathInfo pathInfo; + ComPtr<ISlangBlob> blob; - *ppData = sourceView->getContent().begin(); - *pBytes = (UINT) sourceView->getContentSize(); + SLANG_RETURN_ON_FAIL(m_system.findAndLoadFile(path, includedFromPathInfo.foundPath, pathInfo, blob)); + // Return the data + *outData = blob->getBufferPointer(); + *outSize = (UINT) blob->getBufferSize(); + return S_OK; } @@ -926,6 +881,13 @@ namespace Slang SLANG_UNUSED(pData); return S_OK; } + FxcIncludeHandler(SearchDirectoryList* searchDirectories, ISlangFileSystemExt* fileSystemExt, SourceManager* sourceManager): + m_system(searchDirectories, fileSystemExt, sourceManager) + { + } + + PathInfo m_rootPathInfo; + IncludeSystem m_system; }; SlangResult emitDXBytecodeForEntryPoint( @@ -967,13 +929,9 @@ namespace Slang List<D3D_SHADER_MACRO> dxMacrosStorage; D3D_SHADER_MACRO const* dxMacros = nullptr; - IncludeHandlerImpl includeHandler; - includeHandler.linkage = linkage; - includeHandler.searchDirectories = &linkage->searchDirectories; - - FxcIncludeHandler fxcIncludeHandlerStorage; - FxcIncludeHandler* fxcIncludeHandler = nullptr; - + FxcIncludeHandler fxcIncludeHandlerStorage(&linkage->searchDirectories, linkage->getFileSystemExt(), sink->getSourceManager()); + FxcIncludeHandler* fxcIncludeHandler = &fxcIncludeHandlerStorage; + if(auto translationUnit = findPassThroughTranslationUnit(endToEndReq, entryPointIndex)) { for( auto& define : translationUnit->compileRequest->preprocessorDefinitions ) @@ -996,10 +954,7 @@ namespace Slang dxMacros = dxMacrosStorage.getBuffer(); fxcIncludeHandler = &fxcIncludeHandlerStorage; - fxcIncludeHandler->linkage = linkage; - fxcIncludeHandler->sink = compileRequest->getSink(); - fxcIncludeHandler->includeHandler = &includeHandler; - fxcIncludeHandler->rootPathInfo = translationUnit->m_sourceFiles[0]->getPathInfo(); + fxcIncludeHandlerStorage.m_rootPathInfo = translationUnit->m_sourceFiles[0]->getPathInfo(); } DWORD flags = 0; |
