Skip to content

Commit

Permalink
Shrink Loc from 12 to 4 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jan 25, 2025
1 parent 32883d3 commit 8cf72d8
Show file tree
Hide file tree
Showing 28 changed files with 333 additions and 233 deletions.
1 change: 0 additions & 1 deletion compiler/src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -6324,7 +6324,6 @@ final class CParser(AST) : Parser!AST
while (*p)
++p;
++p; // advance to start of next line
scanloc.linnum = scanloc.linnum + 1;
}

if (newSymbols.length)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ extern (C++) final class Module : Package
checkCompiledImport();
members = p.parseModule();
assert(!p.md); // C doesn't have module declarations
numlines = p.scanloc.linnum;
numlines = p.linnum;
}
else
{
Expand All @@ -831,7 +831,7 @@ extern (C++) final class Module : Package
checkCompiledImport();

members = p.parseModuleContent();
numlines = p.scanloc.linnum;
numlines = p.linnum;
}

/* The symbol table into which the module is to be inserted.
Expand Down
12 changes: 4 additions & 8 deletions compiler/src/dmd/doc.d
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,7 @@ void gendocfile(Module m, const char[] ddoctext, const char* datetime, ErrorSink
OutBuffer buf;
if (m.filetype == FileType.ddoc)
{
const ploc = m.md ? &m.md.loc : &m.loc;
Loc loc = *ploc;
if (!loc.filename)
loc.filename = srcfilename.ptr;
Loc loc = m.md ? m.md.loc : m.loc;

size_t commentlen = m.comment ? strlen(cast(char*)m.comment) : 0;
Dsymbols a;
Expand Down Expand Up @@ -4096,9 +4093,8 @@ size_t endRowAndTable(ref OutBuffer buf, size_t iStart, size_t iEnd, ref Markdow
*/
void highlightText(Scope* sc, Dsymbols* a, Loc loc, ref OutBuffer buf, size_t offset)
{
const incrementLoc = loc.linnum == 0 ? 1 : 0;
loc.linnum = loc.linnum + incrementLoc;
loc.charnum = 0;
loc.nextLine();

//printf("highlightText()\n");
bool leadingBlank = true;
size_t iParagraphStart = offset;
Expand Down Expand Up @@ -4202,7 +4198,7 @@ void highlightText(Scope* sc, Dsymbols* a, Loc loc, ref OutBuffer buf, size_t of
lineQuoted = false;
tableRowDetected = false;
iLineStart = i + 1;
loc.linnum = loc.linnum + incrementLoc;
loc.nextLine();

// update the paragraph start if we just entered a macro
if (previousMacroLevel < macroLevel && iParagraphStart < iLineStart)
Expand Down
41 changes: 24 additions & 17 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1848,8 +1848,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
buf.writeByte(0);
const str = buf.extractSlice()[0 .. len];
const bool doUnittests = global.params.parsingUnittestsRequired();
auto loc = adjustLocForMixin(str, cd.loc, global.params.mixinOut);
scope p = new Parser!ASTCodegen(loc, sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
adjustLocForMixin(str, cd.loc, *p.baseLoc, global.params.mixinOut);
p.linnum = p.baseLoc.startLine;
p.nextToken();

auto d = p.parseDeclDefs(0);
Expand Down Expand Up @@ -5920,34 +5921,40 @@ private CallExp doAtomicOp (string op, Identifier var, Expression arg)
* Set up loc for a parse of a mixin. Append the input text to the mixin.
* Params:
* input = mixin text
* loc = location to adjust
* loc = location of expansion
* baseLoc = location to adjust
* mixinOut = sink for mixin text data
* Returns:
* adjusted loc suitable for Parser
*/

Loc adjustLocForMixin(const(char)[] input, ref const Loc loc, ref Output mixinOut)
void adjustLocForMixin(const(char)[] input, Loc loc, ref BaseLoc baseLoc, ref Output mixinOut)
{
Loc result;
if (mixinOut.doOutput)
{
const lines = mixinOut.bufferLines;
writeMixin(input, loc, mixinOut.bufferLines, *mixinOut.buffer);
result = Loc(mixinOut.name.ptr, lines + 2, loc.charnum);
baseLoc.startLine = lines + 2;
baseLoc.filename = mixinOut.name;
return;
}
else if (loc.filename)

SourceLoc sl = SourceLoc(loc);
if (sl.filename.length == 0)
{
/* Create a pseudo-filename for the mixin string, as it may not even exist
* in the source file.
*/
auto len = strlen(loc.filename) + 7 + (loc.linnum).sizeof * 3 + 1;
char* filename = cast(char*)mem.xmalloc(len);
snprintf(filename, len, "%s-mixin-%d", loc.filename, cast(int)loc.linnum);
result = Loc(filename, loc.linnum, loc.charnum);
// Rare case of compiler-generated mixin exp, e.g. __xtoHash
baseLoc.filename = "";
return;
}
else
result = loc;
return result;

/* Create a pseudo-filename for the mixin string, as it may not even exist
* in the source file.
*/
auto len = sl.filename.length + 7 + (sl.linnum).sizeof * 3 + 1;
char* filename = cast(char*) mem.xmalloc(len);
snprintf(filename, len, "%.*s-mixin-%d", cast(int) sl.filename.length, sl.filename.ptr, cast(int) sl.linnum);
baseLoc.startLine = sl.line;
baseLoc.filename = filename.toDString;
}

/**************************************
Expand Down
8 changes: 2 additions & 6 deletions compiler/src/dmd/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ else
* see verrorReport for arguments
* Returns: true if error handling is done, false to continue printing to stderr
*/
alias DiagnosticHandler = bool delegate(const ref Loc location, Color headerColor, const(char)* header, const(char)* messageFormat, va_list args, const(char)* prefix1, const(char)* prefix2);
alias DiagnosticHandler = bool delegate(const ref SourceLoc location, Color headerColor, const(char)* header, const(char)* messageFormat, va_list args, const(char)* prefix1, const(char)* prefix2);

/**
* The diagnostic handler.
Expand Down Expand Up @@ -671,11 +671,7 @@ private void verrorPrint(const(char)* format, va_list ap, ref ErrorInfo info)

if (diagnosticHandler !is null)
{
Loc diagLoc;
diagLoc.linnum = info.loc.line;
diagLoc.charnum = info.loc.charnum;
diagLoc.filename = (info.loc.filename ~ '\0').ptr;
if (diagnosticHandler(diagLoc, info.headerColor, header, format, ap, info.p1, info.p2))
if (diagnosticHandler(info.loc, info.headerColor, header, format, ap, info.p1, info.p2))
return;
}

Expand Down
7 changes: 4 additions & 3 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,7 @@ private bool checkNogc(FuncDeclaration f, ref Loc loc, Scope* sc)
if (isRootTraitsCompilesScope(sc) ? !sc.func.isNogcBypassingInference() : !sc.func.setGCCall(f))
return false;

if (loc.linnum == 0) // e.g. implicitly generated dtor
if (loc == Loc.initial) // e.g. implicitly generated dtor
loc = sc.func.loc;

// Lowered non-@nogc'd hooks will print their own error message inside of nogc.d (NOGCVisitor.visit(CallExp e)),
Expand Down Expand Up @@ -7654,8 +7654,9 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
const len = buf.length;
const str = buf.extractChars()[0 .. len];
const bool doUnittests = global.params.parsingUnittestsRequired();
auto loc = adjustLocForMixin(str, exp.loc, global.params.mixinOut);
scope p = new Parser!ASTCodegen(loc, sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
adjustLocForMixin(str, exp.loc, *p.baseLoc, global.params.mixinOut);
p.linnum = p.baseLoc.startLine;
p.nextToken();
//printf("p.loc.linnum = %d\n", p.loc.linnum);

Expand Down
26 changes: 13 additions & 13 deletions compiler/src/dmd/frontend.d
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ nothrow:
diagnosticHandler = prevHandler;
}

bool diagHandler(const ref Loc loc, Color headerColor, const(char)* header,
bool diagHandler(const ref SourceLoc loc, Color headerColor, const(char)* header,
const(char)* format, va_list ap, const(char)* p1, const(char)* p2)
{
import core.stdc.string;
Expand Down Expand Up @@ -529,7 +529,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool error(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool error(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports additional details about an error message.
Expand All @@ -543,7 +543,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool errorSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool errorSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports a warning message.
Expand All @@ -557,7 +557,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool warning(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool warning(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports additional details about a warning message.
Expand All @@ -571,7 +571,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool warningSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool warningSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports a deprecation message.
Expand All @@ -585,7 +585,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool deprecation(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool deprecation(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);

/**
Reports additional details about a deprecation message.
Expand All @@ -599,7 +599,7 @@ nothrow:
Returns: false if the message should also be printed to stderr, true otherwise
*/
abstract bool deprecationSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
abstract bool deprecationSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2);
}

/**
Expand Down Expand Up @@ -644,29 +644,29 @@ nothrow:
return deprecationCount_;
}

override bool error(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool error(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
errorCount_++;
return false;
}

override bool errorSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool errorSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
return false;
}

override bool warning(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool warning(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
warningCount_++;
return false;
}

override bool warningSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool warningSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
return false;
}

override bool deprecation(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool deprecation(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
if (useDeprecated == DiagnosticReporting.error)
errorCount_++;
Expand All @@ -675,7 +675,7 @@ nothrow:
return false;
}

override bool deprecationSupplemental(const ref Loc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
override bool deprecationSupplemental(const ref SourceLoc loc, const(char)* format, va_list args, const(char)* p1, const(char)* p2)
{
return false;
}
Expand Down
45 changes: 20 additions & 25 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,23 @@ enum class MessageStyle : uint8_t
struct Loc final
{
private:
uint32_t _linnum;
uint32_t _charnum;
uint32_t fileIndex;
uint32_t index;
public:
static bool showColumns;
static MessageStyle messageStyle;
static void set(bool showColumns, MessageStyle messageStyle);
Loc(const char* filename, uint32_t linnum, uint32_t charnum);
uint32_t charnum() const;
uint32_t charnum(uint32_t num);
uint32_t linnum() const;
uint32_t linnum(uint32_t num);
const char* filename() const;
void filename(const char* name);
const char* toChars(bool showColumns = Loc::showColumns, MessageStyle messageStyle = Loc::messageStyle) const;
bool equals(const Loc& loc) const;
Loc() :
_linnum(),
_charnum(),
fileIndex()
index(0u)
{
}
Loc(uint32_t index) :
index(index)
{}
};

enum class PASS : uint8_t
Expand Down Expand Up @@ -5358,23 +5353,23 @@ struct UnionExp final
private:
union _AnonStruct_u
{
char exp[31LLU];
char integerexp[40LLU];
char errorexp[31LLU];
char exp[23LLU];
char integerexp[32LLU];
char errorexp[23LLU];
char realexp[48LLU];
char complexexp[64LLU];
char symoffexp[64LLU];
char stringexp[59LLU];
char arrayliteralexp[56LLU];
char assocarrayliteralexp[56LLU];
char structliteralexp[76LLU];
char compoundliteralexp[40LLU];
char nullexp[31LLU];
char dotvarexp[49LLU];
char addrexp[40LLU];
char indexexp[58LLU];
char sliceexp[65LLU];
char vectorexp[53LLU];
char symoffexp[56LLU];
char stringexp[51LLU];
char arrayliteralexp[48LLU];
char assocarrayliteralexp[48LLU];
char structliteralexp[68LLU];
char compoundliteralexp[32LLU];
char nullexp[23LLU];
char dotvarexp[41LLU];
char addrexp[32LLU];
char indexexp[50LLU];
char sliceexp[57LLU];
char vectorexp[45LLU];
};
#pragma pack(pop)

Expand Down
18 changes: 2 additions & 16 deletions compiler/src/dmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ typedef unsigned long long uinteger_t;
struct Loc
{
private:
unsigned _linnum;
unsigned _charnum;
unsigned fileIndex;
unsigned int index;
public:
static void set(bool showColumns, MessageStyle messageStyle);

Expand All @@ -428,24 +426,12 @@ struct Loc

Loc()
{
_linnum = 0;
_charnum = 0;
fileIndex = 0;
}

Loc(const char *filename, unsigned linnum, unsigned charnum)
{
this->linnum(linnum);
this->charnum(charnum);
this->filename(filename);
index = 0;
}

uint32_t charnum() const;
uint32_t charnum(uint32_t num);
uint32_t linnum() const;
uint32_t linnum(uint32_t num);
const char *filename() const;
void filename(const char *name);

const char *toChars(
bool showColumns = Loc::showColumns,
Expand Down
Loading

0 comments on commit 8cf72d8

Please sign in to comment.