diff --git a/compiler/src/dmd/hdrgen.d b/compiler/src/dmd/hdrgen.d index 3e264d88d1c5..9515f7c3214b 100644 --- a/compiler/src/dmd/hdrgen.d +++ b/compiler/src/dmd/hdrgen.d @@ -2356,11 +2356,19 @@ private void expressionPrettyPrint(Expression e, ref OutBuffer buf, ref HdrGenSt void visitDsymbol(Dsymbol s) { - // For -vcg-ast, print internal names such as __invariant, __ctor etc. - // This condition is a bit kludge, and can be cleaned up if the - // mutual dependency `AST.toChars <> hdrgen.d` gets refactored - if (hgs.vcg_ast && s.ident && !s.isTemplateInstance() && !s.isTemplateDeclaration()) - buf.put(s.ident.toChars()); + if (hgs.vcg_ast) + { + // For -vcg-ast, print internal names such as __invariant, __ctor etc. + // This condition is a bit kludge, and can be cleaned up if the + // mutual dependency `AST.toChars <> hdrgen.d` gets refactored + auto p = s.toParent(); + if (s.ident && s.ident.toHChars2() != s.ident.toChars()) + buf.put(s.ident.toChars()); + else if (p && (p.isFuncDeclaration() || p.isAggregateDeclaration())) + buf.put(s.toChars()); // function local or fields + else + buf.put(s.toPrettyChars()); // fully qualified name + } else buf.put(s.toChars()); } @@ -2714,6 +2722,15 @@ private void expressionPrettyPrint(Expression e, ref OutBuffer buf, ref HdrGenSt expToBuffer(e.e1, precedence[e.op], buf, hgs); } + void visitBin(BinExp e) + { + expToBuffer(e.e1, precedence[e.op], buf, hgs); + buf.put(' '); + buf.put(EXPtoString(e.op)); + buf.put(' '); + expToBuffer(e.e2, cast(PREC)(precedence[e.op] + 1), buf, hgs); + } + void visitLoweredAssignExp(LoweredAssignExp e) { if (hgs.vcg_ast) @@ -2722,15 +2739,21 @@ private void expressionPrettyPrint(Expression e, ref OutBuffer buf, ref HdrGenSt return; } - visit(cast(BinExp)e); + visitBin(e); } - void visitBin(BinExp e) + + void visitConstructExp(ConstructExp e) { - expToBuffer(e.e1, precedence[e.op], buf, hgs); - buf.put(' '); - buf.put(EXPtoString(e.op)); - buf.put(' '); - expToBuffer(e.e2, cast(PREC)(precedence[e.op] + 1), buf, hgs); + if (hgs.vcg_ast && e.lowering) + return expressionToBuffer(e.lowering, buf, hgs); + visitBin(e); + } + + void visitEqualExp(EqualExp e) + { + if (hgs.vcg_ast && e.lowering) + return expressionToBuffer(e.lowering, buf, hgs); + visitBin(e); } void visitComma(CommaExp e) @@ -2914,6 +2937,9 @@ private void expressionPrettyPrint(Expression e, ref OutBuffer buf, ref HdrGenSt void visitCast(CastExp e) { + if (hgs.vcg_ast && e.lowering) + return expressionToBuffer(e.lowering, buf, hgs); + buf.put("cast("); if (e.to) typeToBuffer(e.to, null, buf, hgs); @@ -3153,6 +3179,9 @@ private void expressionPrettyPrint(Expression e, ref OutBuffer buf, ref HdrGenSt case EXP.question: return visitCond(e.isCondExp()); case EXP.classReference: return visitClassReference(e.isClassReferenceExp()); case EXP.loweredAssignExp: return visitLoweredAssignExp(e.isLoweredAssignExp()); + case EXP.construct: return visitConstructExp(e.isConstructExp()); + case EXP.equal: + case EXP.notEqual: return visitEqualExp(e.isEqualExp()); } } @@ -4198,7 +4227,7 @@ private void visitFuncIdentWithPrefix(TypeFunction t, const Identifier ident, Te else if (hgs.ddoc) buf.put("auto "); if (ident) - buf.put(ident.toHChars2()); + buf.put(hgs.vcg_ast ? ident.toChars() : ident.toHChars2()); if (td) { buf.put('('); diff --git a/compiler/test/compilable/extra-files/vcg-ast.d.cg b/compiler/test/compilable/extra-files/vcg-ast.d.cg index dafad19e7b43..909767c3e3ab 100644 --- a/compiler/test/compilable/extra-files/vcg-ast.d.cg +++ b/compiler/test/compilable/extra-files/vcg-ast.d.cg @@ -97,7 +97,7 @@ T[] values(T)() } void main() { - values(); + vcg.values!(__c_wchar_t).values(); return 0; } import imports.vcg_ast_import; @@ -123,7 +123,7 @@ mixin _d_cmain!(); extern (C) int _Dmain(char[][] args); extern (C) int main(int argc, char** argv) { - return _d_run_main(argc, argv, & _Dmain); + return vcg._d_cmain!()._d_run_main(argc, argv, & _Dmain); } version (Solaris) { diff --git a/compiler/test/compilable/vcg-ast-lowering.d b/compiler/test/compilable/vcg-ast-lowering.d new file mode 100644 index 000000000000..f812b1930825 --- /dev/null +++ b/compiler/test/compilable/vcg-ast-lowering.d @@ -0,0 +1,185 @@ +/* +REQUIRED_ARGS: -vcg-ast -o- +OUTPUT_FILES: compilable/vcg-ast-lowering.d.cg +TEST_OUTPUT: +--- +=== compilable/vcg-ast-lowering.d.cg +module test; +import object; +class C : Object +{ +} +T func(T)(int x) +{ + return 2 * cast(T)x; +} +template fun(T) +{ + T fun1(int x) + { + return 2 * cast(T)x; + } + T fun2(int x) + { + return 2 * cast(T)x; + } +} +auto @system void test() +{ + long x = test.func!long.func(3); + long x1 = test.fun!long.fun1(3); + long x2 = test.fun!long.fun2(3); + int[] a = null; + short[] b = null; + assert(!core.internal.array.equality.__equals!(int, short).__equals(a[], b[])); + Object o = core.lifetime._d_newclassT!(C)._d_newclassT(); + C c = core.internal.cast_._d_cast!(C, Object)._d_cast(o); +} +RTInfo!(C) +{ + enum immutable(void)* RTInfo = null; + +} +func!long +{ + pure nothrow @nogc @safe long func(int x) + { + return 2L * cast(long)x; + } + +} +fun!long +{ + long fun1(int x) + { + return 2L * cast(long)x; + } + long fun2(int x) + { + return 2L * cast(long)x; + } +} +__equals!(int, short) +{ + pure nothrow @nogc @trusted bool __equals(scope int[] lhs, scope short[] rhs) + { + if (lhs.length != rhs.length) + return false; + if (lhs.length == $?:32=0u|64=0LU$) + return true; + alias PureType = bool function(scope int[], scope short[], $?:32=uint|64=ulong$) pure nothrow @nogc @safe; + return (*& isEqual)(lhs, rhs, lhs.length); + } + +} +isEqual!(int, short) +{ + pure nothrow @nogc @safe bool isEqual(scope int[] lhs, scope short[] rhs, $?:32=uint|64=ulong$ length) + { + static ref @trusted at(T)(scope T[] r, size_t i) if (!(is(T == struct) && !is(typeof(T.sizeof)))) + { + static if (is(T == void)) + { + return (cast(ubyte[])r)[i]; + } + else + { + return r[i]; + } + } + { + $?:32=uint|64=ulong$ __key3 = $?:32=0u|64=0LU$; + $?:32=uint|64=ulong$ __limit4 = length; + for (; __key3 < __limit4; __key3 += $?:32=1u|64=1LU$) + { + const const($?:32=uint|64=ulong$) i = __key3; + if (core.internal.array.equality.isEqual!(int, short).isEqual.at!int.at(lhs, i) != cast(int)core.internal.array.equality.isEqual!(int, short).isEqual.at!short.at(rhs, i)) + return false; + } + } + return true; + } + +} +at!int +{ + static pure nothrow @nogc ref @trusted int at(scope int[] r, $?:32=uint|64=ulong$ i) + { + return r[i]; + } + +} +at!short +{ + static pure nothrow @nogc ref @trusted short at(scope short[] r, $?:32=uint|64=ulong$ i) + { + return r[i]; + } + +} +_d_newclassT!(C) +{ + pure nothrow @trusted C _d_newclassT() + { + import core.internal.traits : hasIndirections; + import core.exception : onOutOfMemoryError; + import core.memory : pureMalloc; + import core.memory : GC; + alias BlkAttr = BlkAttr; + const const(void[]) init = C; + void* p = null; + BlkAttr attr = BlkAttr.NONE; + p = malloc(init.length, attr, typeid(C)); + p[0..init.length] = init[]; + return cast(C)p; + } + +} +hasIndirections!(C) +{ + enum bool hasIndirections = true; + +} +_d_cast!(C, Object) +{ + pure nothrow @nogc @trusted void* _d_cast(Object o) + { + return core.internal.cast_._d_class_cast!(C)._d_class_cast(o); + } + +} +_d_class_cast!(C) +{ + pure nothrow @nogc @safe void* _d_class_cast(return scope const(Object) o) + { + return core.internal.cast_._d_class_cast_impl(o, typeid(C)); + } + +} +--- +*/ +// https://github.com/dlang/dmd/issues/23335 + +module test; + +class C {} + +T func(T)(int x) { return 2*cast(T)x; } + +template fun(T) +{ + T fun1(int x) { return 2*cast(T)x; } + T fun2(int x) { return 2*cast(T)x; } +} + +auto test() +{ + auto x = func!long(3); + auto x1 = fun!long.fun1(3); + auto x2 = fun!long.fun2(3); + int[] a; + short[] b; + assert(a[] != b[]); + Object o = new C; + auto c = cast(C)o; +} diff --git a/compiler/test/compilable/vcg_ast_compilable.d b/compiler/test/compilable/vcg_ast_compilable.d index e84846dfbe18..4258498dd15c 100644 --- a/compiler/test/compilable/vcg_ast_compilable.d +++ b/compiler/test/compilable/vcg_ast_compilable.d @@ -17,8 +17,8 @@ void find()(string needle) } void splitter() { - find(3); - find(""); + vcg_ast_compilable.find!int.find(3); + vcg_ast_compilable.find!().find(""); } binaryFun!int {