[WIP] function argument stringification: __ARG_STRING__ (replaced by 7821)#7811
[WIP] function argument stringification: __ARG_STRING__ (replaced by 7821)#7811timotheecour wants to merge 9 commits intodlang:masterfrom
Conversation
|
Thanks for your pull request, @timotheecour! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
|
Wow, I wanted to add this to DMD to, but you beat me to it. dump!(x, y) // x == 5, y == 6
dump(x, y) // arg.1= 5, arg.2 = 6Both have severe downsides,
|
06c384c to
a246ded
Compare
|
yes, that's exactly of of the main use cases i had in mind :) will update commit msg to make it clearer |
|
Please no. This is yet another hack because we don't have AST macros. Yes, I know that we most likely will not get those. I'll give it a fair review anyway. |
changelog/argname.dd
Outdated
|
|
||
| Eg: | ||
| --- | ||
| string log(T)(T a, string name=__ARG_NAME__!a) |
There was a problem hiding this comment.
The code implements __ARG_STRING__.
src/dmd/expression.h
Outdated
| TOK op; // to minimize use of dynamic_cast | ||
| unsigned char size; // # of bytes in Expression so we can copy() it | ||
| unsigned char parens; // if this is a parenthesized expression | ||
| const(char)* stringified; // for function calls (in case of __ARG_NAME__) |
There was a problem hiding this comment.
Same here, the code implements __ARG_STRING__.
test/runnable/testargnames.d
Outdated
| // Checks that no constant folding happens, cf D20180130T161632. | ||
| fun1(1+1+2, `1 + 1 + 2`); | ||
| enum t=44; | ||
| fun1(t+t+t, `t + t + t`); // want: t+t+t |
There was a problem hiding this comment.
Operators should have spaces on each side.
|
I would like to see examples/tests what happens in the following scenarios:
|
a246ded to
be57dc5
Compare
src/dmd/tokens.d
Outdated
| TOK.overloadSet: "__overloadset", | ||
| TOK.file: "__FILE__", | ||
| TOK.fileFullPath: "__FILE_FULL_PATH__", | ||
| TOK.argumentName: "__ARG_STRING__", |
There was a problem hiding this comment.
The naming is inconsistent. The token is called argumentName while the symbol used in the code is called __ARG_STRING__.
src/dmd/parse.d
Outdated
| /***************************************** | ||
| * Parses default argument initializer expression that is an assign expression, | ||
| * with special handling for __FILE__, __FILE_DIR__, __LINE__, __MODULE__, __FUNCTION__, and __PRETTY_FUNCTION__. | ||
| * with special handling for __FILE__, __FILE_DIR__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__, __ARG_NAMES__. |
There was a problem hiding this comment.
__ARG_NAMES__ -> __ARG_STRING__.
src/dmd/expression.d
Outdated
|
|
||
| /*********************************************************** | ||
| */ | ||
| extern (C++) final class ArgnameInitExp : DefaultInitExp |
There was a problem hiding this comment.
Now there are three different names in play here: ArgnameInitExp, __ARG_STRING__ and argumentName. Pick one of them and use it consistently. Please also avoid abbreviations.
There was a problem hiding this comment.
changed to more consistent naming:
__ARG_STRING__
argString
ArgStringInitExp
isArgStringInitExp
resolveArgString
I did use commonly used abbreviation though, __ARGUMENT_STRING__ would be too long IMO (and some ppl had suggesed ARG also)
src/dmd/expressionsem.d
Outdated
| assert(nargs>0); | ||
| bool found=false; | ||
| // TODO: should Parameters._foreach be used? | ||
| for(int u=0;u<nparams;u++) |
There was a problem hiding this comment.
Use foreach instead of for.
src/dmd/expressionsem.d
Outdated
| return; | ||
| } | ||
|
|
||
| for(int i=0;i<(exp.arguments ? exp.arguments.dim : 0) ; i++){ |
There was a problem hiding this comment.
asDArray can be used instead.
There was a problem hiding this comment.
not used anymore
|
There are commits from other people that are already merged. |
49e2a9d to
90fbb28
Compare
fixed |
I am disallowing this use case; ARG_STRING is useful mostly as a default function argument. If you have a good use case for that (which one, I'd be curious?) we could always add that later.
as noted in PR msg, this is on TODO list but could be done in subsequent PR while being backward compatible
added this: see tests |
…mplex expressions
I'm not arguing for or a against, you just need a test that enforces the current behavior.
Same here, test. |
|
I don't want to completely ruin this PR, but what about a |
| pound, | ||
|
|
||
| // 239 | ||
| // 241 |
There was a problem hiding this comment.
You only added one token, why does the count go up by two?
There was a problem hiding this comment.
IIRC fileFullPath had been added without upading these numbers in the past, so i also correct that; however I'd super like to just get rid of these numbers, they serve no purpose and get out of sync easily; shd be done in a separate PR though; ok to remove in another PR?
There was a problem hiding this comment.
Fair enough, just making sure you hadn't typo'd. I think they have some use when trying to look up what Tok you have in some function but I'll leave it up to other to determine what to do.
There was a problem hiding this comment.
I see; in this case one could use instead arbitrary increasing numbers at sparse intervals, it's much more robust (and more stable across different compiler versions)
enum TOK : int
{
reserved = 0,
// we can reset numbers arbitrarily
leftParentheses = 100,
rightParentheses, // will be 101count)
leftBracket = 200,
}
|
How does this work with varadic parameters? what about getting stringification of multiple parameters in a |
There's even |
after discussion on slack, the only difference with proposed PR would be a minor syntactic change ( NOTE: I'm closing in favor of #7821 which proposes a more robust implementation that avoids technical issues with not having source code lying around, eg FILE and enum's got folded too early |
Stringify caller's function argument, analog to C's
#stringification macro (except it's type safe).A main use case is to allow finally writing nice logging and debug tools.
Purely library based alternatives all have drawbacks, see #7811 (comment) for example.
It also works with UFCS.
Eg:
NOTE:
earg.stringified = earg.toChars;for all function arguments only when attempting to match (viacallMatch) against a function that has__ARG_STRING__. Caveat:enum foldingwill happen before that, soenum x=3; assert(log(x) == "3: 3");instead ofassert(log(x) == "x: 3");; but the behavior is correct for non-enum's (egstatic const)EDIT: closing in favor of #7821