www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2698] New: Parameterised identifier

reply d-bugmail puremagic.com writes:

           Summary: Parameterised identifier
           Product: D
           Version: 2.025
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: daniel.keep+d.puremagic.com gmail.com

Proposal: add the following to the D grammar:

    IdentiferStart IdentifierChars

    "__ident" "(" AssignExpression ")"

This allows for two use-cases.  Firstly, it allows the definition of symbols
that do not have valid D identifiers.  The pressing example from the efforts to
port DMD to Mac OSX is the following [1]:


This is an issue with the pthread library in 10.5.  D cannot link to these
functions directly, and a C shim would have to be built.  Instead, this
proposal would allow for the following syntax:

  extern(C) void __ident("blah$UNIX2003")(int);

thus removing the need for a shim.  This need could also be filled by replacing
"AssignExpression" with "StringLiteral", which I assume would be easier to

The second use-case is in compile-time generated code.  An example might be
generating a set of property accessors and storage, given a name and type.

  mixin Property!("foo", int);

The problem here is that actually creating the functions and storage is fiddly.
 You have to do this within the template or CTFE:

  mixin(`int `~name~`() { return _`~name~`_storage; }`);

Because of how mixin decls work, you have to quote out the entire function just
to replace the identifiers.  The proposal would allow for this:

  int __ident(name)() { return __ident("_"~name~"storage"); }

Admittedly, this is actually longer than the above, but it has the distinct
advantage of not requiring the programmer to stuff about with quoting and
unquoting their code.

In terms of mangling, I believe the symbol should be treated as not being
mangled yet.  If control over this is required (I can't imagine it being of use
except in very extreme circumstances), another proposal could be made to extend
the syntax to allow:

  __ident(name, cast(bool) should_name_be_mangled)


Feb 28 2009
parent d-bugmail puremagic.com writes:

------- Comment #1 from clugdbug yahoo.com.au  2009-03-04 08:07 -------
You missed a third use-case: giving identifiers the names of keywords. This may
be significant when interfacing with other languages. (Or even, earlier
versions of D!).

extern(C) {
int __identifier("pure")(int);

__identifier is a keyword in MSVC for this reason.

(BTW, if you _were_ going to support the name mangling case, a better syntax
would probably be:
__identifier("name", "mangle")
One situation where this would be useful would be in direct linking to C++
compilers other than DMC/GCC. But I don't think that should be considered at
all in this proposal).

Mar 04 2009