Prepare import paths mechanism for metadata

This commit is contained in:
Richard (Rikki) Andrew Cattermole 2024-12-30 22:23:36 +13:00 committed by Nicholas Wilson
parent 605ca0f009
commit 12d4f3e4d6
11 changed files with 109 additions and 36 deletions

View file

@ -51,7 +51,7 @@ DArray!ubyte preprocess(FileName csrcfile, ref const Loc loc, ref OutBuffer defi
{
/* Look for "importc.h" by searching along import path.
*/
const(char)* importc_h = findImportcH(global.path[]);
const(char)* importc_h = findImportcH(global.importPaths[]);
if (importc_h)
{

View file

@ -310,8 +310,9 @@ extern (C++) class Package : ScopeDsymbol
packages ~= s.ident;
reverse(packages);
if (Module.find(getFilename(packages, ident)))
Module.load(Loc.initial, packages, this.ident);
ImportPathInfo pathThatFoundThis;
if (Module.find(getFilename(packages, ident), pathThatFoundThis))
Module.load(Loc.initial, packages, this.ident, pathThatFoundThis);
else
isPkgMod = PKG.package_;
}
@ -499,12 +500,18 @@ extern (C++) final class Module : Package
static const(char)* find(const(char)* filename)
{
return find(filename.toDString).ptr;
ImportPathInfo pathThatFoundThis; // is this needed? In fact is this function needed still???
return find(filename.toDString, pathThatFoundThis).ptr;
}
extern (D) static const(char)[] find(const(char)[] filename)
extern (D) static const(char)[] find(const(char)[] filename, out ImportPathInfo pathThatFoundThis)
{
return global.fileManager.lookForSourceFile(filename, global.path[]);
ptrdiff_t whichPathFoundThis;
const(char)[] ret = global.fileManager.lookForSourceFile(filename, global.path[], whichPathFoundThis);
if (whichPathFoundThis >= 0)
pathThatFoundThis = global.path[whichPathFoundThis];
return ret;
}
extern (C++) static Module load(const ref Loc loc, Identifiers* packages, Identifier ident)
@ -512,7 +519,7 @@ extern (C++) final class Module : Package
return load(loc, packages ? (*packages)[] : null, ident);
}
extern (D) static Module load(const ref Loc loc, Identifier[] packages, Identifier ident)
extern (D) static Module load(const ref Loc loc, Identifier[] packages, Identifier ident, ImportPathInfo pathInfo = ImportPathInfo.init)
{
//printf("Module::load(ident = '%s')\n", ident.toChars());
// Build module filename by turning:
@ -521,11 +528,17 @@ extern (C++) final class Module : Package
// foo\bar\baz
const(char)[] filename = getFilename(packages, ident);
// Look for the source file
if (const result = find(filename))
ImportPathInfo importPathThatFindUs;
if (const result = find(filename, importPathThatFindUs))
{
filename = result; // leaks
pathInfo = importPathThatFindUs;
}
auto m = new Module(loc, filename, ident, 0, 0);
// TODO: apply import path information (pathInfo) on to module
if (!m.read(loc))
return null;
if (global.params.v.verbose)
@ -662,7 +675,7 @@ extern (C++) final class Module : Package
if (global.path.length)
{
foreach (i, p; global.path[])
fprintf(stderr, "import path[%llu] = %s\n", cast(ulong)i, p);
fprintf(stderr, "import path[%llu] = %s\n", cast(ulong)i, p.path);
}
else
{

View file

@ -159,16 +159,20 @@ nothrow:
* Does not open the file.
* Params:
* filename = as supplied by the user
* paths = paths to look for filename
* pathsInfo = pathsInfo to look for filename with metadata
* whichPathFoundThis = Which path from `path` was used in determining the output path, or -1 if unknown.
* Returns:
* the found file name or
* `null` if it is not different from filename.
*/
const(char)[] lookForSourceFile(const char[] filename, const char*[] paths)
const(char)[] lookForSourceFile(const char[] filename, const ImportPathInfo[] pathsInfo, out ptrdiff_t whichPathFoundThis)
{
//printf("lookForSourceFile(`%.*s`)\n", cast(int)filename.length, filename.ptr);
/* Search along paths[] for .di file, then .d file.
/* Search along pathsInfo[] for .di file, then .d file.
*/
whichPathFoundThis = -1;
// see if we should check for the module locally.
bool checkLocal = pathCache.pathExists(filename);
const sdi = FileName.forceExt(filename, hdr_ext);
@ -206,11 +210,11 @@ nothrow:
if (FileName.absolute(filename))
return null;
if (!paths.length)
if (!pathsInfo.length)
return null;
foreach (entry; paths)
foreach (pathIndex, entry; pathsInfo)
{
const p = entry.toDString();
const p = entry.path.toDString();
const(char)[] n = FileName.combine(p, sdi);
@ -225,6 +229,7 @@ nothrow:
n = FileName.combine(p, sd);
if (FileName.exists(n) == 1) {
whichPathFoundThis = pathIndex;
return n;
}
FileName.free(n.ptr);
@ -236,11 +241,15 @@ nothrow:
if (pathCache.isExistingPath(n))
{
const n2i = FileName.combine(n, package_di);
if (FileName.exists(n2i) == 1)
if (FileName.exists(n2i) == 1) {
whichPathFoundThis = pathIndex;
return n2i;
}
FileName.free(n2i.ptr);
const n2 = FileName.combine(n, package_d);
if (FileName.exists(n2) == 1) {
whichPathFoundThis = pathIndex;
return n2;
}
FileName.free(n2.ptr);
@ -258,18 +267,20 @@ nothrow:
if (FileName.exists(sc) == 1)
return sc;
scope(exit) FileName.free(sc.ptr);
foreach (entry; paths)
foreach (pathIndex, entry; pathsInfo)
{
const p = entry.toDString();
const p = entry.path.toDString();
const(char)[] n = FileName.combine(p, si);
if (FileName.exists(n) == 1) {
whichPathFoundThis = pathIndex;
return n;
}
FileName.free(n.ptr);
n = FileName.combine(p, sc);
if (FileName.exists(n) == 1) {
whichPathFoundThis = pathIndex;
return n;
}
FileName.free(n.ptr);

View file

@ -208,11 +208,10 @@ Params:
*/
void addImport(const(char)[] path)
{
import dmd.globals : global;
import dmd.arraytypes : Strings;
import dmd.globals : global, ImportPathInfo;
import std.string : toStringz;
global.path.push(path.toStringz);
global.path.push(ImportPathInfo(path.toStringz));
}
/**

View file

@ -8330,6 +8330,18 @@ struct Verbose final
{}
};
struct ImportPathInfo final
{
const char* path;
ImportPathInfo() :
path()
{
}
ImportPathInfo(const char* path) :
path(path)
{}
};
struct Param final
{
bool obj;
@ -8386,7 +8398,7 @@ struct Param final
CLIIdentifierTable cIdentifierTable;
_d_dynamicArray< const char > argv0;
Array<const char* > modFileAliasStrings;
Array<const char* > imppath;
Array<ImportPathInfo > imppath;
Array<const char* > fileImppath;
_d_dynamicArray< const char > objdir;
_d_dynamicArray< const char > objname;
@ -8500,7 +8512,7 @@ struct Param final
timeTraceFile()
{
}
Param(bool obj, bool multiobj = false, bool trace = false, bool tracegc = false, bool vcg_ast = false, DiagnosticReporting useDeprecated = (DiagnosticReporting)1u, bool useUnitTests = false, bool useInline = false, bool release = false, bool preservePaths = false, DiagnosticReporting useWarnings = (DiagnosticReporting)2u, bool cov = false, uint8_t covPercent = 0u, bool ctfe_cov = false, bool ignoreUnsupportedPragmas = true, bool useModuleInfo = true, bool useTypeInfo = true, bool useExceptions = true, bool useGC = true, bool betterC = false, bool addMain = false, bool allInst = false, bool bitfields = false, CppStdRevision cplusplus = (CppStdRevision)201103u, Help help = Help(), Verbose v = Verbose(), FeatureState useDIP25 = (FeatureState)2u, FeatureState useDIP1000 = (FeatureState)0u, bool ehnogc = false, bool useDIP1021 = false, FeatureState fieldwise = (FeatureState)0u, bool fixAliasThis = false, FeatureState rvalueRefParam = (FeatureState)0u, FeatureState safer = (FeatureState)0u, FeatureState noSharedAccess = (FeatureState)0u, bool previewIn = false, bool inclusiveInContracts = false, bool shortenedMethods = true, bool fixImmutableConv = false, bool fix16997 = true, FeatureState dtorFields = (FeatureState)0u, FeatureState systemVariables = (FeatureState)0u, CHECKENABLE useInvariants = (CHECKENABLE)0u, CHECKENABLE useIn = (CHECKENABLE)0u, CHECKENABLE useOut = (CHECKENABLE)0u, CHECKENABLE useArrayBounds = (CHECKENABLE)0u, CHECKENABLE useAssert = (CHECKENABLE)0u, CHECKENABLE useSwitchError = (CHECKENABLE)0u, CHECKENABLE boundscheck = (CHECKENABLE)0u, CHECKACTION checkAction = (CHECKACTION)0u, CLIIdentifierTable dIdentifierTable = (CLIIdentifierTable)0u, CLIIdentifierTable cIdentifierTable = (CLIIdentifierTable)0u, _d_dynamicArray< const char > argv0 = {}, Array<const char* > modFileAliasStrings = Array<const char* >(), Array<const char* > imppath = Array<const char* >(), Array<const char* > fileImppath = Array<const char* >(), _d_dynamicArray< const char > objdir = {}, _d_dynamicArray< const char > objname = {}, _d_dynamicArray< const char > libname = {}, Output ddoc = Output(), Output dihdr = Output(), Output cxxhdr = Output(), Output json = Output(), JsonFieldFlags jsonFieldFlags = (JsonFieldFlags)0u, Output makeDeps = Output(), Output mixinOut = Output(), Output moduleDeps = Output(), uint32_t debuglevel = 0u, uint32_t versionlevel = 0u, bool run = false, Array<const char* > runargs = Array<const char* >(), Array<const char* > cppswitches = Array<const char* >(), const char* cpp = nullptr, Array<const char* > objfiles = Array<const char* >(), Array<const char* > linkswitches = Array<const char* >(), Array<bool > linkswitchIsForCC = Array<bool >(), Array<const char* > libfiles = Array<const char* >(), Array<const char* > dllfiles = Array<const char* >(), _d_dynamicArray< const char > deffile = {}, _d_dynamicArray< const char > resfile = {}, _d_dynamicArray< const char > exefile = {}, _d_dynamicArray< const char > mapfile = {}, bool fullyQualifiedObjectFiles = false, bool timeTrace = false, uint32_t timeTraceGranularityUs = 500u, const char* timeTraceFile = nullptr) :
Param(bool obj, bool multiobj = false, bool trace = false, bool tracegc = false, bool vcg_ast = false, DiagnosticReporting useDeprecated = (DiagnosticReporting)1u, bool useUnitTests = false, bool useInline = false, bool release = false, bool preservePaths = false, DiagnosticReporting useWarnings = (DiagnosticReporting)2u, bool cov = false, uint8_t covPercent = 0u, bool ctfe_cov = false, bool ignoreUnsupportedPragmas = true, bool useModuleInfo = true, bool useTypeInfo = true, bool useExceptions = true, bool useGC = true, bool betterC = false, bool addMain = false, bool allInst = false, bool bitfields = false, CppStdRevision cplusplus = (CppStdRevision)201103u, Help help = Help(), Verbose v = Verbose(), FeatureState useDIP25 = (FeatureState)2u, FeatureState useDIP1000 = (FeatureState)0u, bool ehnogc = false, bool useDIP1021 = false, FeatureState fieldwise = (FeatureState)0u, bool fixAliasThis = false, FeatureState rvalueRefParam = (FeatureState)0u, FeatureState safer = (FeatureState)0u, FeatureState noSharedAccess = (FeatureState)0u, bool previewIn = false, bool inclusiveInContracts = false, bool shortenedMethods = true, bool fixImmutableConv = false, bool fix16997 = true, FeatureState dtorFields = (FeatureState)0u, FeatureState systemVariables = (FeatureState)0u, CHECKENABLE useInvariants = (CHECKENABLE)0u, CHECKENABLE useIn = (CHECKENABLE)0u, CHECKENABLE useOut = (CHECKENABLE)0u, CHECKENABLE useArrayBounds = (CHECKENABLE)0u, CHECKENABLE useAssert = (CHECKENABLE)0u, CHECKENABLE useSwitchError = (CHECKENABLE)0u, CHECKENABLE boundscheck = (CHECKENABLE)0u, CHECKACTION checkAction = (CHECKACTION)0u, CLIIdentifierTable dIdentifierTable = (CLIIdentifierTable)0u, CLIIdentifierTable cIdentifierTable = (CLIIdentifierTable)0u, _d_dynamicArray< const char > argv0 = {}, Array<const char* > modFileAliasStrings = Array<const char* >(), Array<ImportPathInfo > imppath = Array<ImportPathInfo >(), Array<const char* > fileImppath = Array<const char* >(), _d_dynamicArray< const char > objdir = {}, _d_dynamicArray< const char > objname = {}, _d_dynamicArray< const char > libname = {}, Output ddoc = Output(), Output dihdr = Output(), Output cxxhdr = Output(), Output json = Output(), JsonFieldFlags jsonFieldFlags = (JsonFieldFlags)0u, Output makeDeps = Output(), Output mixinOut = Output(), Output moduleDeps = Output(), uint32_t debuglevel = 0u, uint32_t versionlevel = 0u, bool run = false, Array<const char* > runargs = Array<const char* >(), Array<const char* > cppswitches = Array<const char* >(), const char* cpp = nullptr, Array<const char* > objfiles = Array<const char* >(), Array<const char* > linkswitches = Array<const char* >(), Array<bool > linkswitchIsForCC = Array<bool >(), Array<const char* > libfiles = Array<const char* >(), Array<const char* > dllfiles = Array<const char* >(), _d_dynamicArray< const char > deffile = {}, _d_dynamicArray< const char > resfile = {}, _d_dynamicArray< const char > exefile = {}, _d_dynamicArray< const char > mapfile = {}, bool fullyQualifiedObjectFiles = false, bool timeTrace = false, uint32_t timeTraceGranularityUs = 500u, const char* timeTraceFile = nullptr) :
obj(obj),
multiobj(multiobj),
trace(trace),
@ -8595,7 +8607,8 @@ struct Global final
_d_dynamicArray< const char > inifilename;
_d_dynamicArray< const char > copyright;
_d_dynamicArray< const char > written;
Array<const char* > path;
Array<ImportPathInfo > path;
Array<const char* > importPaths;
Array<const char* > filePath;
char datetime[26LLU];
CompileEnv compileEnv;
@ -8629,6 +8642,7 @@ struct Global final
copyright(73, "Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved"),
written(24, "written by Walter Bright"),
path(),
importPaths(),
filePath(),
compileEnv(),
params(),
@ -8649,11 +8663,12 @@ struct Global final
preprocess()
{
}
Global(_d_dynamicArray< const char > inifilename, _d_dynamicArray< const char > copyright = { 73, "Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved" }, _d_dynamicArray< const char > written = { 24, "written by Walter Bright" }, Array<const char* > path = Array<const char* >(), Array<const char* > filePath = Array<const char* >(), CompileEnv compileEnv = CompileEnv(), Param params = Param(), uint32_t errors = 0u, uint32_t deprecations = 0u, uint32_t warnings = 0u, uint32_t gag = 0u, uint32_t gaggedErrors = 0u, uint32_t gaggedWarnings = 0u, void* console = nullptr, Array<Identifier* > versionids = Array<Identifier* >(), Array<Identifier* > debugids = Array<Identifier* >(), bool hasMainFunction = false, uint32_t varSequenceNumber = 1u, FileManager* fileManager = nullptr, ErrorSink* errorSink = nullptr, ErrorSink* errorSinkNull = nullptr, DArray<uint8_t >(*preprocess)(FileName , const Loc& , OutBuffer& ) = nullptr) :
Global(_d_dynamicArray< const char > inifilename, _d_dynamicArray< const char > copyright = { 73, "Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved" }, _d_dynamicArray< const char > written = { 24, "written by Walter Bright" }, Array<ImportPathInfo > path = Array<ImportPathInfo >(), Array<const char* > importPaths = Array<const char* >(), Array<const char* > filePath = Array<const char* >(), CompileEnv compileEnv = CompileEnv(), Param params = Param(), uint32_t errors = 0u, uint32_t deprecations = 0u, uint32_t warnings = 0u, uint32_t gag = 0u, uint32_t gaggedErrors = 0u, uint32_t gaggedWarnings = 0u, void* console = nullptr, Array<Identifier* > versionids = Array<Identifier* >(), Array<Identifier* > debugids = Array<Identifier* >(), bool hasMainFunction = false, uint32_t varSequenceNumber = 1u, FileManager* fileManager = nullptr, ErrorSink* errorSink = nullptr, ErrorSink* errorSinkNull = nullptr, DArray<uint8_t >(*preprocess)(FileName , const Loc& , OutBuffer& ) = nullptr) :
inifilename(inifilename),
copyright(copyright),
written(written),
path(path),
importPaths(importPaths),
filePath(filePath),
compileEnv(compileEnv),
params(params),

View file

@ -1103,7 +1103,7 @@ Ldone:
{
printedMain = true;
auto name = mod.srcfile.toChars();
auto path = FileName.searchPath(global.path, name, true);
auto path = FileName.searchPath(global.importPaths, name, true);
message("entry %-10s\t%s", type, path ? path : name);
}
}

View file

@ -151,6 +151,10 @@ extern(C++) struct Verbose
}
}
extern (C++) struct ImportPathInfo {
const(char)* path; // char*'s of where to look for import modules
}
/// Put command line switches in here
extern (C++) struct Param
{
@ -223,7 +227,7 @@ extern (C++) struct Param
const(char)[] argv0; // program name
Array!(const(char)*) modFileAliasStrings; // array of char*'s of -I module filename alias strings
Array!(const(char)*) imppath; // array of char*'s of where to look for import modules
Array!(ImportPathInfo) imppath; // array of import path information of where to look for import modules
Array!(const(char)*) fileImppath; // array of char*'s of where to look for file import modules
const(char)[] objdir; // .obj/.lib file output directory
const(char)[] objname; // .obj file output name
@ -291,7 +295,8 @@ extern (C++) struct Global
string copyright = "Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved";
string written = "written by Walter Bright";
Array!(const(char)*) path; /// Array of char*'s which form the import lookup path
Array!(ImportPathInfo) path; /// Array of path informations which form the import lookup path
Array!(const(char)*) importPaths; /// Array of char*'s which form the import lookup path without metadata
Array!(const(char)*) filePath; /// Array of char*'s which form the file import lookup path
private enum string _version = import("VERSION");

View file

@ -155,6 +155,11 @@ struct Verbose
unsigned errorSupplementCount();
};
struct ImportPathInfo
{
const char* path;
};
// Put command line switches in here
struct Param
{
@ -227,7 +232,7 @@ struct Param
DString argv0; // program name
Array<const char *> modFileAliasStrings; // array of char*'s of -I module filename alias strings
Array<const char *> imppath; // array of char*'s of where to look for import modules
Array<ImportPathInfo> imppath; // array of import path information of where to look for import modules
Array<const char *> fileImppath; // array of char*'s of where to look for file import modules
DString objdir; // .obj/.lib file output directory
DString objname; // .obj file output name
@ -315,8 +320,9 @@ struct Global
const DString copyright;
const DString written;
Array<const char *> path; // Array of char*'s which form the import lookup path
Array<const char *> filePath; // Array of char*'s which form the file import lookup path
Array<ImportPathInfo> path; // Array of path informations which form the import lookup path
Array<const char *> importPaths; // Array of char*'s which form the import lookup path without metadata
Array<const char *> filePath; // Array of char*'s which form the file import lookup path
char datetime[26]; /// string returned by ctime()
CompileEnv compileEnv;

View file

@ -906,7 +906,7 @@ public:
arrayStart();
foreach (importPath; global.params.imppath[])
{
item(importPath.toDString);
item(importPath.path.toDString);
}
arrayEnd();

View file

@ -64,6 +64,7 @@ import dmd.root.response;
import dmd.root.rmem;
import dmd.root.string;
import dmd.root.stringtable;
import dmd.root.array;
import dmd.semantic2;
import dmd.semantic3;
import dmd.target;
@ -397,7 +398,30 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
// Build import search path
static void buildPath(ref Strings imppath, ref Strings result)
static void buildImportPath(ref Array!ImportPathInfo imppath, ref Array!ImportPathInfo result, ref Strings pathsOnlyResult)
{
Array!ImportPathInfo array;
Strings pathsOnlyArray;
foreach (entry; imppath)
{
int sink(const(char)* p) nothrow
{
ImportPathInfo temp = entry;
temp.path = p;
array.push(temp);
return 0;
}
FileName.splitPath(&sink, entry.path);
FileName.appendSplitPath(entry.path, pathsOnlyArray);
}
result.append(&array);
pathsOnlyResult.append(&pathsOnlyArray);
}
static void buildFileImportPath(ref Strings imppath, ref Strings result)
{
Strings array;
foreach (const path; imppath)
@ -413,8 +437,8 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
atexit(&flushMixins); // see comment for flushMixins
}
scope(exit) flushMixins();
buildPath(params.imppath, global.path);
buildPath(params.fileImppath, global.filePath);
buildImportPath(params.imppath, global.path, global.importPaths);
buildFileImportPath(params.fileImppath, global.filePath);
if (params.timeTrace)
{

View file

@ -1572,7 +1572,7 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
params.useUnitTests = true;
else if (p[1] == 'I') // https://dlang.org/dmd.html#switch-I
{
params.imppath.push(p + 2 + (p[2] == '='));
params.imppath.push(ImportPathInfo(p + 2 + (p[2] == '=')));
}
else if (p[1] == 'm' && p[2] == 'v' && p[3] == '=') // https://dlang.org/dmd.html#switch-mv
{