Skip to content

Commit

Permalink
Merge pull request #2173 from 9rnsr/fix10334
Browse files Browse the repository at this point in the history
Issue 10334 - ddoc should prefer simple syntax for template instantiations with one parameter
  • Loading branch information
AndrejMitrovic committed Jun 18, 2013
2 parents 1f0af1f + 905c4dd commit 26022fb
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 83 deletions.
75 changes: 39 additions & 36 deletions src/template.c
Expand Up @@ -6507,23 +6507,54 @@ void TemplateInstance::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
{
Identifier *id = name;
buf->writestring(id->toChars());
buf->writestring("!(");
toCBufferTiargs(buf, hgs);
}

void TemplateInstance::toCBufferTiargs(OutBuffer *buf, HdrGenState *hgs)
{
buf->writeByte('!');
if (nest)
buf->writestring("...");
buf->writestring("(...)");
else
{
if (tiargs->dim == 1)
{
Object *oarg = (*tiargs)[0];
if (Type *t = isType(oarg))
{
if (t->isTypeBasic() ||
t->equals(Type::tstring) ||
t->ty == Tident && ((TypeIdentifier *)t)->idents.dim == 0)
{
buf->writestring(t->toChars());
return;
}
}
else if (Expression *e = isExpression(oarg))
{
if (e->op == TOKint64 || // IntegerExp(10, true, false, 'c')
e->op == TOKfloat64 || // RealExp(3.14, 1.4i)
e->op == TOKnull || // NullExp
e->op == TOKstring || // StringExp
e->op == TOKthis)
{
buf->writestring(e->toChars());
return;
}
}
}
buf->writeByte('(');
nest++;
Objects *args = tiargs;
for (size_t i = 0; i < args->dim; i++)
for (size_t i = 0; i < tiargs->dim; i++)
{
if (i)
buf->writestring(", ");
Object *oarg = (*args)[i];
Object *oarg = (*tiargs)[i];
ObjectToCBuffer(buf, hgs, oarg);
}
nest--;
buf->writeByte(')');
}
buf->writeByte(')');
}


Expand Down Expand Up @@ -7032,36 +7063,8 @@ void TemplateMixin::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
buf->writestring("mixin ");

tqual->toCBuffer(buf, NULL, hgs);
buf->writestring("!(");
if (tiargs)
{
for (size_t i = 0; i < tiargs->dim; i++)
{ if (i)
buf->writestring(", ");
Object *oarg = (*tiargs)[i];
Type *t = isType(oarg);
Expression *e = isExpression(oarg);
Dsymbol *s = isDsymbol(oarg);
if (t)
t->toCBuffer(buf, NULL, hgs);
else if (e)
e->toCBuffer(buf, hgs);
else if (s)
{
char *p = s->ident ? s->ident->toChars() : s->toChars();
buf->writestring(p);
}
else if (!oarg)
{
buf->writestring("NULL");
}
else
{
assert(0);
}
}
}
buf->writebyte(')');
toCBufferTiargs(buf, hgs);

if (ident)
{
buf->writebyte(' ');
Expand Down
1 change: 1 addition & 0 deletions src/template.h
Expand Up @@ -335,6 +335,7 @@ class TemplateInstance : public ScopeDsymbol
void semantic3(Scope *sc);
void inlineScan();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
void toCBufferTiargs(OutBuffer *buf, HdrGenState *hgs);
Dsymbol *toAlias(); // resolve real symbol
const char *kind();
bool oneMember(Dsymbol **ps, Identifier *ident);
Expand Down
27 changes: 27 additions & 0 deletions test/compilable/ddoc10334.d
@@ -0,0 +1,27 @@
// PERMUTE_ARGS:
// REQUIRED_ARGS: -D -Ddtest_results/compilable -o-
// POST_SCRIPT: compilable/extra-files/ddocAny-postscript.sh 10334

module ddoc10334;

template Foo10334(T) if (Bar10334!()) {} ///
template Foo10334(T) if (Bar10334!100) {} ///
template Foo10334(T) if (Bar10334!3.14) {} ///
template Foo10334(T) if (Bar10334!"str") {} ///
template Foo10334(T) if (Bar10334!1.4i) {} ///
template Foo10334(T) if (Bar10334!null) {} ///
template Foo10334(T) if (Bar10334!true) {} ///
template Foo10334(T) if (Bar10334!false) {} ///
template Foo10334(T) if (Bar10334!'A') {} ///
template Foo10334(T) if (Bar10334!int) {} ///
template Foo10334(T) if (Bar10334!string) {} ///
template Foo10334(T) if (Bar10334!([1,2,3])) {} ///
template Foo10334(T) if (Bar10334!(Baz10334!())) {} ///
template Foo10334(T) if (Bar10334!(Baz10334!T)) {} ///
template Foo10334(T) if (Bar10334!(Baz10334!100)) {} ///
template Foo10334(T) if (Bar10334!(.foo)) {} ///

template Test10334(T...) {} ///
mixin Test10334!int a; ///
mixin Test10334!(int,long) b; ///
mixin Test10334!"str" c; ///
4 changes: 2 additions & 2 deletions test/compilable/extra-files/ddoc10325.html
Expand Up @@ -4,10 +4,10 @@
</head><body>
<h1>ddoc10325</h1>
<br><br>
<dl><dt><big><a name="templ"></a>template <u>templ</u>(T...) if (someConstraint!(T))</big></dt>
<dl><dt><big><a name="templ"></a>template <u>templ</u>(T...) if (someConstraint!T)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="foo"></a>void <u>foo</u>(T)(T <i>t</i>) if (someConstraint!(T));
<dt><big><a name="foo"></a>void <u>foo</u>(T)(T <i>t</i>) if (someConstraint!T);
</big></dt>
<dd><br><br>
</dd>
Expand Down
61 changes: 61 additions & 0 deletions test/compilable/extra-files/ddoc10334.html
@@ -0,0 +1,61 @@
<html><head>
<META http-equiv="content-type" content="text/html; charset=utf-8">
<title>ddoc10334</title>
</head><body>
<h1>ddoc10334</h1>
<br><br>
<dl><dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!())</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!100)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!3.14)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!"str")</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!1.4i)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!null)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!true)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!false)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!'A')</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!int)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!string)</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!([1, 2, 3]))</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!(Baz10334!()))</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!(Baz10334!T))</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!(Baz10334!100))</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Foo10334"></a>template <u>Foo10334</u>(T) if (Bar10334!(.foo))</big></dt>
<dd><br><br>
</dd>
<dt><big><a name="Test10334"></a>template <u>Test10334</u>(T...)</big></dt>
<dd><br><br>
</dd>
</dl>

<hr><small>Page generated by <a href="http://dlang.org/ddoc.html">Ddoc</a>. </small>
</body></html>
2 changes: 1 addition & 1 deletion test/compilable/extra-files/ddoc13.html
Expand Up @@ -31,7 +31,7 @@ <h1>ddoc13</h1>
</big></dt>
<dd>delta<br><br>

<dl><dt><big><a name="e"></a>B!(W) <u>e</u>(X)(C!(V) <i>c</i>, X[] <i>x</i>...);
<dl><dt><big><a name="e"></a>B!W <u>e</u>(X)(C!V <i>c</i>, X[] <i>x</i>...);
</big></dt>
<dd>epsilon<br><br>

Expand Down
83 changes: 70 additions & 13 deletions test/compilable/extra-files/header.di
Expand Up @@ -15,12 +15,12 @@ template Foo(T, int V)
{
void foo(...)
{
static if (is(Object _ : X!(TL), alias X, TL...))
static if (is(Object _ : X!TL, alias X, TL...))
{
}

auto x = __traits(hasMember, Object, "noMember");
auto y = is(Object : X!(TL), alias X, TL...);
auto y = is(Object : X!TL, alias X, TL...);
assert(!x && !y, "message");
S s = {1, 2};
auto a = [1, 2, 3];
Expand Down Expand Up @@ -192,17 +192,17 @@ class Test
template A(T)
{
}
alias A!(uint) getHUint;
alias A!(int) getHInt;
alias A!(float) getHFloat;
alias A!(ulong) getHUlong;
alias A!(long) getHLong;
alias A!(double) getHDouble;
alias A!(byte) getHByte;
alias A!(ubyte) getHUbyte;
alias A!(short) getHShort;
alias A!(ushort) getHUShort;
alias A!(real) getHReal;
alias A!uint getHUint;
alias A!int getHInt;
alias A!float getHFloat;
alias A!ulong getHUlong;
alias A!long getHLong;
alias A!double getHDouble;
alias A!byte getHByte;
alias A!ubyte getHUbyte;
alias A!short getHShort;
alias A!ushort getHUShort;
alias A!real getHReal;
}
template templ(T)
{
Expand Down Expand Up @@ -365,3 +365,60 @@ version (unittest)
extern (C) {}
align{}
}
template Foo10334(T) if (Bar10334!())
{
}
template Foo10334(T) if (Bar10334!100)
{
}
template Foo10334(T) if (Bar10334!3.14)
{
}
template Foo10334(T) if (Bar10334!"str")
{
}
template Foo10334(T) if (Bar10334!1.4i)
{
}
template Foo10334(T) if (Bar10334!null)
{
}
template Foo10334(T) if (Bar10334!true)
{
}
template Foo10334(T) if (Bar10334!false)
{
}
template Foo10334(T) if (Bar10334!'A')
{
}
template Foo10334(T) if (Bar10334!int)
{
}
template Foo10334(T) if (Bar10334!string)
{
}
template Foo10334(T) if (Bar10334!this)
{
}
template Foo10334(T) if (Bar10334!([1, 2, 3]))
{
}
template Foo10334(T) if (Bar10334!(Baz10334!()))
{
}
template Foo10334(T) if (Bar10334!(Baz10334!T))
{
}
template Foo10334(T) if (Bar10334!(Baz10334!100))
{
}
template Foo10334(T) if (Bar10334!(.foo))
{
}
template Test10334(T...)
{
}
mixin Test10334!int a;
mixin Test10334!(int, long) b;
mixin Test10334!"str" c;

0 comments on commit 26022fb

Please sign in to comment.