Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog/argname.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Stringify caller's function argument, analog to C's `#` stringification macro (except it's type safe).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly are you adding? __traits(getCallerSource, a) ? Or is it more than that?


NOTE: it also works with UFCS, with tuples (string static array) and non-tuples (string)

Eg:
---
string logSimple(T...)(T a, string[T.length] names = __traits(getCallerSource, a))
{
import std.conv;
return text(names, ": ", a); // see testargnames for a better log function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use four spaces for indentation + don't reference to DMD's testsuite in the changelog.

}

double x = 1.5;
logSimple(x * 2, 'a') // ["x * 2", "'a'"]: 3a
logSimple(__LINE__) // ["__LINE__"]: 9
---
13 changes: 13 additions & 0 deletions src/dmd/astbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -5248,6 +5248,19 @@ struct ASTBase
}
}

extern (C++) final class ArgStringInitExp : DefaultInitExp
{
extern (D) this(Loc loc)
{
super(loc, TOK.getCallerSource, __traits(classInstanceSize, ArgStringInitExp));
}

override void accept(Visitor v)
{
v.visit(this);
}
}

extern (C++) final class LineInitExp : DefaultInitExp
{
extern (D) this(Loc loc)
Expand Down
2 changes: 2 additions & 0 deletions src/dmd/backend/cgobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ struct Loc
char *filename;
unsigned linnum;
unsigned charnum;
unsigned bytes; // TODO: needed?

Loc(int y, int x)
{
linnum = y;
charnum = x;
filename = NULL;
bytes = 0;
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/dmd/dinifile.d
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void parseConfFile(StringTable* environment, const(char)* filename, const(char)*
{
if (!writeToEnv(environment, strdup(pn)))
{
error(Loc(filename, lineNum, 0), "Use `NAME=value` syntax, not `%s`", pn);
error(Loc(filename, lineNum, 0, cast(uint)linestart), "Use `NAME=value` syntax, not `%s`", pn);
fatal();
}
static if (LOG)
Expand Down
19 changes: 15 additions & 4 deletions src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ extern (C++) final class Module : Package
modules = new DsymbolTable();
}

extern (C++) void releaseResources()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global comment: Add Ddoc comment for all new functions.

{
// printf("Module::releaseResources %s\n", this.toChars());
if (srcfile._ref == 0)
.free(srcfile.buffer);
srcfile.buffer = null;
srcfile.len = 0;
}

extern (C++) static __gshared AggregateDeclaration moduleinfo;

const(char)* arg; // original argument name
Expand Down Expand Up @@ -875,10 +884,12 @@ extern (C++) final class Module : Package
if (p.errors)
++global.errors;
}
if (srcfile._ref == 0)
.free(srcfile.buffer);
srcfile.buffer = null;
srcfile.len = 0;

if(global.params.disposeSrcContent)
{
releaseResources();
}

/* The symbol table into which the module is to be inserted.
*/
DsymbolTable dst;
Expand Down
79 changes: 79 additions & 0 deletions src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,8 @@ extern (C++) abstract class Expression : RootObject
return buf.extractString();
}

// TODO: https://issues.dlang.org/show_bug.cgi?id=18371 Issue 18371 - allow default parameters after `...` (not just template variadics, which are ok now)
version(all)
final void error(const(char)* format, ...) const
{
if (type != Type.terror)
Expand All @@ -1614,6 +1616,26 @@ extern (C++) abstract class Expression : RootObject
va_end(ap);
}
}
else
{
pragma(inline, true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why force inline this?

extern(D)
final void error(string file=__FILE__, int line=__LINE__)(const(char)* format, ...) const
{
version(with_debug){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where/what is with_debug?

import std.conv:text;
writelnL(file, ":", line);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where/what is writelnL()?

}
if (type != Type.terror)
{
va_list ap;
va_start(ap, format);
.verror(loc, format, ap);
va_end(ap);
}
}
}


final void warning(const(char)* format, ...) const
{
Expand Down Expand Up @@ -2441,6 +2463,12 @@ extern (C++) abstract class Expression : RootObject
return false;
}

// TODO: can we avoid this?
ArgStringInitExp isArgStringInitExp()
{
return null;
}

final Expression op_overload(Scope* sc)
{
return .op_overload(this, sc);
Expand Down Expand Up @@ -7096,6 +7124,57 @@ extern (C++) final class FileInitExp : DefaultInitExp
}
}

/***********************************************************
*/
extern (C++) final class ArgStringInitExp : DefaultInitExp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is an ArgStringInitExp? There's no documentation here.

{
// function parameter we want stringified
// NOTE: redundant with exp.args[0],toChars
Identifier ident;
TraitsExp exp;

extern (D) this(const ref Loc loc)
{
super(loc, TOK.getCallerSource, __traits(classInstanceSize, ArgStringInitExp));
}

void setIdent(Identifier ident)
{
this.ident = ident;
}

override ArgStringInitExp isArgStringInitExp()
{
return this;
}

extern (D) Expression resolveArgString(const ref Loc loc, Scope* sc, string value)
{
Expression e = new StringExp(loc, cast(void*)value.ptr, value.length);
e = e.expressionSemantic(sc);
e = e.castTo(sc, type);
return e;
}

extern (D) Expression resolveArgStrings(const ref Loc loc, Scope* sc, string[] values)
{
auto elements = new Expressions();
elements.setDim(values.length);
foreach(j, value; values){
(*elements)[j] = new StringExp(loc, cast(void*)value.ptr, value.length);
}
Expression e = new ArrayLiteralExp(loc, elements);
e = e.expressionSemantic(sc);
e = e.castTo(sc, type);
return e;
}

override void accept(Visitor v)
{
v.visit(this);
}
}

/***********************************************************
*/
extern (C++) final class LineInitExp : DefaultInitExp
Expand Down
6 changes: 6 additions & 0 deletions src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,12 @@ class FileInitExp : public DefaultInitExp
void accept(Visitor *v) { v->visit(this); }
};

class ArgStringInitExp : public DefaultInitExp
{
public:
void accept(Visitor *v) { v->visit(this); }
};

class LineInitExp : public DefaultInitExp
{
public:
Expand Down
Loading