From 8bc2f56e1cd3b69d6e9a861209bcbae96c55cf72 Mon Sep 17 00:00:00 2001 From: Eduard Staniloiu Date: Fri, 9 Aug 2019 18:25:25 +0300 Subject: [PATCH 1/5] Make RootObject equals and toChars const --- src/dmd/astbase.d | 8 ++++---- src/dmd/cond.d | 6 +++--- src/dmd/declaration.d | 4 ++-- src/dmd/dsymbol.d | 4 ++-- src/dmd/dtemplate.d | 18 +++++++++++------- src/dmd/dversion.d | 2 +- src/dmd/expression.d | 33 +++++++++++++++++---------------- src/dmd/func.d | 4 ++-- src/dmd/hdrgen.d | 29 +++++++++++++++++------------ src/dmd/init.d | 2 +- src/dmd/mtype.d | 11 ++++++----- src/dmd/root/array.d | 14 +++++++++++--- src/dmd/root/rootobject.d | 6 +++--- src/dmd/statement.d | 2 +- 14 files changed, 81 insertions(+), 62 deletions(-) diff --git a/src/dmd/astbase.d b/src/dmd/astbase.d index 0bc773300586..3b211bc39464 100644 --- a/src/dmd/astbase.d +++ b/src/dmd/astbase.d @@ -335,7 +335,7 @@ struct ASTBase this.comment = Lexer.combineComments(this.comment.toDString(), comment.toDString(), true); } - override const(char)* toChars() + override const(char)* toChars() const { return ident ? ident.toChars() : "__anonymous"; } @@ -2768,7 +2768,7 @@ struct ASTBase this.ty = ty; } - override const(char)* toChars() + override const(char)* toChars() const { return "type"; } @@ -6295,7 +6295,7 @@ struct ASTBase return DYNCAST.tuple; } - override const(char)* toChars() + override const(char)* toChars() const { return objects.toChars(); } @@ -6323,7 +6323,7 @@ struct ASTBase this.isdeprecated = isdeprecated; } - extern (C++) const(char)* toChars() + extern (C++) const(char)* toChars() const { OutBuffer buf; if (packages && packages.dim) diff --git a/src/dmd/cond.d b/src/dmd/cond.d index b4f03c704762..3843369a2e81 100644 --- a/src/dmd/cond.d +++ b/src/dmd/cond.d @@ -546,7 +546,7 @@ extern (C++) final class DebugCondition : DVCondition v.visit(this); } - override const(char)* toChars() + override const(char)* toChars() const { return ident ? ident.toChars() : "debug".ptr; } @@ -821,7 +821,7 @@ extern (C++) final class VersionCondition : DVCondition v.visit(this); } - override const(char)* toChars() + override const(char)* toChars() const { return ident ? ident.toChars() : "version".ptr; } @@ -887,7 +887,7 @@ extern (C++) final class StaticIfCondition : Condition v.visit(this); } - override const(char)* toChars() + override const(char)* toChars() const { return exp ? exp.toChars() : "static if".ptr; } diff --git a/src/dmd/declaration.d b/src/dmd/declaration.d index 2466547401c0..c899cdf44df5 100644 --- a/src/dmd/declaration.d +++ b/src/dmd/declaration.d @@ -980,7 +980,7 @@ extern (C++) final class OverDeclaration : Declaration return "overload alias"; // todo } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -1718,7 +1718,7 @@ extern (C++) class TypeInfoDeclaration : VarDeclaration assert(0); // should never be produced by syntax } - override final const(char)* toChars() + override final const(char)* toChars() const { //printf("TypeInfoDeclaration::toChars() tinfo = %s\n", tinfo.toChars()); OutBuffer buf; diff --git a/src/dmd/dsymbol.d b/src/dmd/dsymbol.d index 8988c95f0eaf..379a7b678c09 100644 --- a/src/dmd/dsymbol.d +++ b/src/dmd/dsymbol.d @@ -272,7 +272,7 @@ extern (C++) class Dsymbol : ASTNode return new Dsymbol(ident); } - override const(char)* toChars() + override const(char)* toChars() const { return ident ? ident.toChars() : "__anonymous"; } @@ -296,7 +296,7 @@ extern (C++) class Dsymbol : ASTNode return getLoc().toChars(); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; diff --git a/src/dmd/dtemplate.d b/src/dmd/dtemplate.d index 6520d7285247..f36e0cc02fbe 100644 --- a/src/dmd/dtemplate.d +++ b/src/dmd/dtemplate.d @@ -494,7 +494,7 @@ extern (C++) final class Tuple : RootObject return DYNCAST.tuple; } - override const(char)* toChars() + override const(char)* toChars() const { return objects.toChars(); } @@ -657,7 +657,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol return (onemember && onemember.isAggregateDeclaration()) ? onemember.kind() : "template"; } - override const(char)* toChars() + override const(char)* toChars() const { if (literal) return Dsymbol.toChars(); @@ -669,7 +669,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol buf.writeByte('('); for (size_t i = 0; i < parameters.dim; i++) { - TemplateParameter tp = (*parameters)[i]; + const TemplateParameter tp = (*parameters)[i]; if (i) buf.writestring(", "); .toCBuffer(tp, &buf, &hgs); @@ -678,7 +678,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol if (onemember) { - FuncDeclaration fd = onemember.isFuncDeclaration(); + const FuncDeclaration fd = onemember.isFuncDeclaration(); if (fd && fd.type) { TypeFunction tf = cast(TypeFunction)fd.type; @@ -5267,7 +5267,11 @@ extern (C++) class TemplateParameter : ASTNode abstract bool hasDefaultArg(); - override const(char)* toChars() { return this.ident.toChars(); } + override const(char)* toChars() const + { + return this.ident.toChars(); + } + override DYNCAST dyncast() const pure @nogc nothrow @safe { return DYNCAST.templateparameter; @@ -5826,7 +5830,7 @@ extern (C++) class TemplateInstance : ScopeDsymbol return true; } - override const(char)* toChars() + override const(char)* toChars() const { OutBuffer buf; toCBufferInstance(this, &buf); @@ -7455,7 +7459,7 @@ extern (C++) final class TemplateMixin : TemplateInstance members.foreachDsymbol( (s) { s.setFieldOffset(ad, poffset, isunion); } ); } - override const(char)* toChars() + override const(char)* toChars() const { OutBuffer buf; toCBufferInstance(this, &buf); diff --git a/src/dmd/dversion.d b/src/dmd/dversion.d index df23a4000b7e..454baf0a70fd 100644 --- a/src/dmd/dversion.d +++ b/src/dmd/dversion.d @@ -139,7 +139,7 @@ extern (C++) final class VersionSymbol : Dsymbol return ds; } - override const(char)* toChars() nothrow + override const(char)* toChars() const nothrow { if (ident) return ident.toChars(); diff --git a/src/dmd/expression.d b/src/dmd/expression.d index 793055ce921c..8f7b44a13be8 100644 --- a/src/dmd/expression.d +++ b/src/dmd/expression.d @@ -710,7 +710,7 @@ extern (C++) abstract class Expression : ASTNode return DYNCAST.expression; } - override const(char)* toChars() + override const(char)* toChars() const { OutBuffer buf; HdrGenState hgs; @@ -1679,7 +1679,7 @@ extern (C++) final class IntegerExp : Expression emplaceExp!(IntegerExp)(pue, loc, value, type); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -1923,7 +1923,7 @@ extern (C++) final class RealExp : Expression emplaceExp!(RealExp)(pue, loc, value, type); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -1998,7 +1998,7 @@ extern (C++) final class ComplexExp : Expression emplaceExp!(ComplexExp)(pue, loc, value, type); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -2214,7 +2214,7 @@ extern (C++) final class NullExp : Expression this.type = type; } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (auto e = o.isExpression()) { @@ -2311,7 +2311,7 @@ extern (C++) final class StringExp : Expression emplaceExp!(StringExp)(pue, loc, string, len); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { //printf("StringExp::equals('%s') %s\n", o.toChars(), toChars()); if (auto e = o.isExpression()) @@ -2694,7 +2694,7 @@ extern (C++) final class TupleExp : Expression return new TupleExp(loc, e0 ? e0.syntaxCopy() : null, arraySyntaxCopy(exps)); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -2781,7 +2781,7 @@ extern (C++) final class ArrayLiteralExp : Expression arraySyntaxCopy(elements)); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -2796,11 +2796,12 @@ extern (C++) final class ArrayLiteralExp : Expression { return false; } - foreach (i, e1; *elements) + Expressions* unqualElements = cast(Expressions*) elements; + foreach (i, e1; *unqualElements) { Expression e2 = (*ae.elements)[i]; if (!e1) - e1 = basis; + e1 = cast(Expression) basis; if (!e2) e2 = ae.basis; if (e1 != e2 && (!e1 || !e2 || !e1.equals(e2))) @@ -2908,7 +2909,7 @@ extern (C++) final class AssocArrayLiteralExp : Expression this.values = values; } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -3009,7 +3010,7 @@ extern (C++) final class StructLiteralExp : Expression return new StructLiteralExp(loc, sd, cast(Expressions*)elements, stype); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -3443,7 +3444,7 @@ extern (C++) final class VarExp : SymbolExp return new VarExp(loc, var, hasOverloads); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -3576,7 +3577,7 @@ extern (C++) final class FuncExp : Expression assert(fd.fbody); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; @@ -3816,7 +3817,7 @@ extern (C++) final class FuncExp : Expression return m; } - override const(char)* toChars() + override const(char)* toChars() const { return fd.toChars(); } @@ -4407,7 +4408,7 @@ extern (C++) final class CompileExp : Expression return new CompileExp(loc, arraySyntaxCopy(exps)); } - override bool equals(RootObject o) + override bool equals(RootObject o) const { if (this == o) return true; diff --git a/src/dmd/func.d b/src/dmd/func.d index e1e71ae89ff8..d889bce622ce 100644 --- a/src/dmd/func.d +++ b/src/dmd/func.d @@ -558,14 +558,14 @@ extern (C++) class FuncDeclaration : Declaration return HiddenParameters.init; } - override final bool equals(RootObject o) + override final bool equals(RootObject o) const { if (this == o) return true; if (auto s = isDsymbol(o)) { - auto fd1 = this; + auto fd1 = cast(FuncDeclaration) this; auto fd2 = s.isFuncDeclaration(); if (!fd2) return false; diff --git a/src/dmd/hdrgen.d b/src/dmd/hdrgen.d index 62d5fe12ac46..d7c57e871262 100644 --- a/src/dmd/hdrgen.d +++ b/src/dmd/hdrgen.d @@ -2629,15 +2629,17 @@ public: } } -void toCBuffer(Statement s, OutBuffer* buf, HdrGenState* hgs) +void toCBuffer(const Statement s, OutBuffer* buf, HdrGenState* hgs) { scope v = new StatementPrettyPrintVisitor(buf, hgs); - s.accept(v); + Statement unqualS = cast(Statement) s; + unqualS.accept(v); } -void toCBuffer(Type t, OutBuffer* buf, const Identifier ident, HdrGenState* hgs) +void toCBuffer(const Type t, OutBuffer* buf, const Identifier ident, HdrGenState* hgs) { - typeToBuffer(t, ident, buf, hgs); + Type unqualT = cast(Type) t; + typeToBuffer(unqualT, ident, buf, hgs); } void toCBuffer(Dsymbol s, OutBuffer* buf, HdrGenState* hgs) @@ -2647,17 +2649,18 @@ void toCBuffer(Dsymbol s, OutBuffer* buf, HdrGenState* hgs) } // used from TemplateInstance::toChars() and TemplateMixin::toChars() -void toCBufferInstance(TemplateInstance ti, OutBuffer* buf, bool qualifyTypes = false) +void toCBufferInstance(const TemplateInstance ti, OutBuffer* buf, bool qualifyTypes = false) { HdrGenState hgs; hgs.fullQual = qualifyTypes; scope v = new DsymbolPrettyPrintVisitor(buf, &hgs); - v.visit(ti); + v.visit(cast(TemplateInstance) ti); } -void toCBuffer(Initializer iz, OutBuffer* buf, HdrGenState* hgs) +void toCBuffer(const Initializer iz, OutBuffer* buf, HdrGenState* hgs) { - initializerToBuffer(iz, buf, hgs); + Initializer unqualIz = cast(Initializer) iz; + initializerToBuffer(unqualIz, buf, hgs); } bool stcToBuffer(OutBuffer* buf, StorageClass stc) @@ -2885,10 +2888,11 @@ void functionToBufferWithIdent(TypeFunction tf, OutBuffer* buf, const(char)* ide visitFuncIdentWithPostfix(tf, ident.toDString(), buf, &hgs); } -void toCBuffer(Expression e, OutBuffer* buf, HdrGenState* hgs) +void toCBuffer(const Expression e, OutBuffer* buf, HdrGenState* hgs) { scope v = new ExpressionPrettyPrintVisitor(buf, hgs); - e.accept(v); + Expression unqualE = cast(Expression) e; + unqualE.accept(v); } /************************************************** @@ -2907,10 +2911,11 @@ void argExpTypesToCBuffer(OutBuffer* buf, Expressions* arguments) } } -void toCBuffer(TemplateParameter tp, OutBuffer* buf, HdrGenState* hgs) +void toCBuffer(const TemplateParameter tp, OutBuffer* buf, HdrGenState* hgs) { scope v = new TemplateParameterPrettyPrintVisitor(buf, hgs); - tp.accept(v); + TemplateParameter unqualTP = cast(TemplateParameter) tp; + unqualTP.accept(v); } void arrayObjectsToBuffer(OutBuffer* buf, Objects* objects) diff --git a/src/dmd/init.d b/src/dmd/init.d index 023e7dccf73d..e2e690899178 100644 --- a/src/dmd/init.d +++ b/src/dmd/init.d @@ -63,7 +63,7 @@ extern (C++) class Initializer : ASTNode this.kind = kind; } - override final const(char)* toChars() + override final const(char)* toChars() const { OutBuffer buf; HdrGenState hgs; diff --git a/src/dmd/mtype.d b/src/dmd/mtype.d index 937eb744e78d..0983745392d6 100644 --- a/src/dmd/mtype.d +++ b/src/dmd/mtype.d @@ -784,7 +784,7 @@ extern (C++) abstract class Type : ASTNode /******************************** * For pretty-printing a type. */ - final override const(char)* toChars() + final override const(char)* toChars() const { OutBuffer buf; buf.reserve(16); @@ -2382,11 +2382,12 @@ extern (C++) abstract class Type : ASTNode /************************** * Return type with the top level of it being mutable. */ - Type toHeadMutable() + inout(Type) toHeadMutable() inout { if (!mod) return this; - return mutableOf(); + Type unqualThis = cast(Type) this; + return cast(inout(Type)) unqualThis.mutableOf(); } inout(ClassDeclaration) isClassHandle() inout @@ -5754,7 +5755,7 @@ extern (C++) final class TypeStruct : Type return wm; } - override Type toHeadMutable() + override inout(Type) toHeadMutable() inout { return this; } @@ -6068,7 +6069,7 @@ extern (C++) final class TypeClass : Type return wm; } - override Type toHeadMutable() + override inout(Type) toHeadMutable() inout { return this; } diff --git a/src/dmd/root/array.d b/src/dmd/root/array.d index b99be854837a..5f8f37d21259 100644 --- a/src/dmd/root/array.d +++ b/src/dmd/root/array.d @@ -45,7 +45,7 @@ public: mem.xfree(data); } ///returns elements comma separated in [] - extern(D) const(char)[] toString() + extern(D) const(char)[] toString() const { static if (is(typeof(T.init.toString()))) { @@ -53,7 +53,15 @@ public: size_t len = 2; // [ and ] foreach (u; 0 .. length) { - buf[u] = data[u].toString(); + static if (is(typeof(this) == const(A), A) && is (A == Array!UnqualT, UnqualT)) + { + UnqualT* unqualData = cast(UnqualT*) data; + buf[u] = unqualData[u].toString(); + } + else + { + buf[u] = data[u].toString(); + } len += buf[u].length + 1; //length + ',' or null terminator } char[] str = (cast(char*)mem.xmalloc(len))[0..len]; @@ -79,7 +87,7 @@ public: } } ///ditto - const(char)* toChars() + const(char)* toChars() const { return toString.ptr; } diff --git a/src/dmd/root/rootobject.d b/src/dmd/root/rootobject.d index dfe21773d389..7d74d3535db0 100644 --- a/src/dmd/root/rootobject.d +++ b/src/dmd/root/rootobject.d @@ -42,18 +42,18 @@ extern (C++) class RootObject { } - bool equals(RootObject o) + bool equals(RootObject o) const { return o is this; } - const(char)* toChars() + const(char)* toChars() const { assert(0); } /// - extern(D) const(char)[] toString() + extern(D) const(char)[] toString() const { import core.stdc.string : strlen; auto p = this.toChars(); diff --git a/src/dmd/statement.d b/src/dmd/statement.d index 5d4ab8c71cf3..472e69ecee4d 100644 --- a/src/dmd/statement.d +++ b/src/dmd/statement.d @@ -166,7 +166,7 @@ extern (C++) abstract class Statement : ASTNode return b; } - override final const(char)* toChars() + override final const(char)* toChars() const { HdrGenState hgs; OutBuffer buf; From bc04fce1fe7a2090a013a67ec6b3414bc8980ed5 Mon Sep 17 00:00:00 2001 From: Eduard Staniloiu Date: Sat, 10 Aug 2019 00:16:59 +0300 Subject: [PATCH 2/5] qf --- src/dmd/astbase.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dmd/astbase.d b/src/dmd/astbase.d index 3b211bc39464..69380811aab7 100644 --- a/src/dmd/astbase.d +++ b/src/dmd/astbase.d @@ -6330,7 +6330,7 @@ struct ASTBase { for (size_t i = 0; i < packages.dim; i++) { - Identifier pid = (*packages)[i]; + const Identifier pid = (*packages)[i]; buf.writestring(pid.toString()); buf.writeByte('.'); } From e3101ca41cc833bf8f283df6a2a395f9d62a9fa5 Mon Sep 17 00:00:00 2001 From: Eduard Staniloiu Date: Sat, 10 Aug 2019 01:04:30 +0300 Subject: [PATCH 3/5] Update header files --- src/dmd/attrib.h | 6 +++--- src/dmd/ctfe.h | 6 +++--- src/dmd/declaration.h | 10 +++++----- src/dmd/dsymbol.h | 4 ++-- src/dmd/expression.h | 26 +++++++++++++------------- src/dmd/identifier.h | 6 +++--- src/dmd/init.h | 2 +- src/dmd/mtype.h | 8 ++++---- src/dmd/root/array.h | 2 +- src/dmd/statement.h | 2 +- src/dmd/template.h | 8 ++++---- src/dmd/version.h | 4 ++-- 12 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/dmd/attrib.h b/src/dmd/attrib.h index 6cf23fed6aca..4b2dfa9e02d6 100644 --- a/src/dmd/attrib.h +++ b/src/dmd/attrib.h @@ -77,7 +77,7 @@ class LinkDeclaration : public AttribDeclaration static LinkDeclaration *create(LINK p, Dsymbols *decl); Dsymbol *syntaxCopy(Dsymbol *s); Scope *newScope(Scope *sc); - const char *toChars(); + const char *toChars() const; void accept(Visitor *v) { v->visit(this); } }; @@ -88,7 +88,7 @@ class CPPMangleDeclaration : public AttribDeclaration Dsymbol *syntaxCopy(Dsymbol *s); Scope *newScope(Scope *sc); - const char *toChars(); + const char *toChars() const; void accept(Visitor *v) { v->visit(this); } }; @@ -99,7 +99,7 @@ class CPPNamespaceDeclaration : public AttribDeclaration Dsymbol *syntaxCopy(Dsymbol *s); Scope *newScope(Scope *sc); - const char *toChars(); + const char *toChars() const; void accept(Visitor *v) { v->visit(this); } }; diff --git a/src/dmd/ctfe.h b/src/dmd/ctfe.h index d09e5f58c08d..51da94c9a643 100644 --- a/src/dmd/ctfe.h +++ b/src/dmd/ctfe.h @@ -37,7 +37,7 @@ class VoidInitExp : public Expression public: VarDeclaration *var; - const char *toChars(); + const char *toChars() const; void accept(Visitor *v) { v->visit(this); } }; @@ -49,7 +49,7 @@ class ThrownExceptionExp : public Expression { public: ClassReferenceExp *thrown; // the thing being tossed - const char *toChars(); + const char *toChars() const; void accept(Visitor *v) { v->visit(this); } }; @@ -60,5 +60,5 @@ class ThrownExceptionExp : public Expression class CTFEExp : public Expression { public: - const char *toChars(); + const char *toChars() const; }; diff --git a/src/dmd/declaration.h b/src/dmd/declaration.h index 75f0f24d52a2..f3bc01aa9709 100644 --- a/src/dmd/declaration.h +++ b/src/dmd/declaration.h @@ -193,7 +193,7 @@ class OverDeclaration : public Declaration bool hasOverloads; const char *kind() const; - bool equals(RootObject *o); + bool equals(RootObject *o) const; bool overloadInsert(Dsymbol *s); Dsymbol *toAlias(); @@ -286,7 +286,7 @@ class TypeInfoDeclaration : public VarDeclaration static TypeInfoDeclaration *create(Type *tinfo); Dsymbol *syntaxCopy(Dsymbol *); - const char *toChars(); + const char *toChars() const; TypeInfoDeclaration *isTypeInfoDeclaration() { return this; } void accept(Visitor *v) { v->visit(this); } @@ -561,7 +561,7 @@ class FuncDeclaration : public Declaration Dsymbol *syntaxCopy(Dsymbol *); bool functionSemantic(); bool functionSemantic3(); - bool equals(RootObject *o); + bool equals(RootObject *o) const; int overrides(FuncDeclaration *fd); int findVtblIndex(Dsymbols *vtbl, int dim, bool fix17349 = true); @@ -658,7 +658,7 @@ class CtorDeclaration : public FuncDeclaration bool isCpCtor; Dsymbol *syntaxCopy(Dsymbol *); const char *kind() const; - const char *toChars(); + const char *toChars() const; bool isVirtual() const; bool addPreInvariant(); bool addPostInvariant(); @@ -685,7 +685,7 @@ class DtorDeclaration : public FuncDeclaration public: Dsymbol *syntaxCopy(Dsymbol *); const char *kind() const; - const char *toChars(); + const char *toChars() const; bool isVirtual() const; bool addPreInvariant(); bool addPostInvariant(); diff --git a/src/dmd/dsymbol.h b/src/dmd/dsymbol.h index 386d70980b0e..c2cfa0831623 100644 --- a/src/dmd/dsymbol.h +++ b/src/dmd/dsymbol.h @@ -163,11 +163,11 @@ class Dsymbol : public ASTNode UnitTestDeclaration *ddocUnittest; // !=NULL means there's a ddoc unittest associated with this symbol (only use this with ddoc) static Dsymbol *create(Identifier *); - const char *toChars(); + const char *toChars() const; virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments Loc getLoc(); const char *locToChars(); - bool equals(RootObject *o); + bool equals(RootObject *o) const; virtual bool isAnonymous(); void error(const Loc &loc, const char *format, ...); void error(const char *format, ...); diff --git a/src/dmd/expression.h b/src/dmd/expression.h index 8497225bd614..fa94411567a7 100644 --- a/src/dmd/expression.h +++ b/src/dmd/expression.h @@ -80,7 +80,7 @@ class Expression : public ASTNode // kludge for template.isExpression() DYNCAST dyncast() const { return DYNCAST_EXPRESSION; } - const char *toChars(); + const char *toChars() const; void error(const char *format, ...) const; void warning(const char *format, ...) const; void deprecation(const char *format, ...) const; @@ -234,7 +234,7 @@ class IntegerExp : public Expression static IntegerExp *create(Loc loc, dinteger_t value, Type *type); static void emplace(UnionExp *pue, Loc loc, dinteger_t value, Type *type); - bool equals(RootObject *o); + bool equals(RootObject *o) const; dinteger_t toInteger(); real_t toReal(); real_t toImaginary(); @@ -264,7 +264,7 @@ class RealExp : public Expression static RealExp *create(Loc loc, real_t value, Type *type); static void emplace(UnionExp *pue, Loc loc, real_t value, Type *type); - bool equals(RootObject *o); + bool equals(RootObject *o) const; dinteger_t toInteger(); uinteger_t toUInteger(); real_t toReal(); @@ -281,7 +281,7 @@ class ComplexExp : public Expression static ComplexExp *create(Loc loc, complex_t value, Type *type); static void emplace(UnionExp *pue, Loc loc, complex_t value, Type *type); - bool equals(RootObject *o); + bool equals(RootObject *o) const; dinteger_t toInteger(); uinteger_t toUInteger(); real_t toReal(); @@ -344,7 +344,7 @@ class NullExp : public Expression public: unsigned char committed; // !=0 if type is committed - bool equals(RootObject *o); + bool equals(RootObject *o) const; bool isBool(bool result); StringExp *toStringExp(); void accept(Visitor *v) { v->visit(this); } @@ -364,7 +364,7 @@ class StringExp : public Expression static StringExp *create(Loc loc, void *s, size_t len); static void emplace(UnionExp *pue, Loc loc, char *s); static void emplace(UnionExp *pue, Loc loc, void *s, size_t len); - bool equals(RootObject *o); + bool equals(RootObject *o) const; StringExp *toStringExp(); StringExp *toUTF8(Scope *sc); bool isBool(bool result); @@ -396,7 +396,7 @@ class TupleExp : public Expression static TupleExp *create(Loc loc, Expressions *exps); TupleExp *toTupleExp(); Expression *syntaxCopy(); - bool equals(RootObject *o); + bool equals(RootObject *o) const; void accept(Visitor *v) { v->visit(this); } }; @@ -411,7 +411,7 @@ class ArrayLiteralExp : public Expression static ArrayLiteralExp *create(Loc loc, Expressions *elements); static void emplace(UnionExp *pue, Loc loc, Expressions *elements); Expression *syntaxCopy(); - bool equals(RootObject *o); + bool equals(RootObject *o) const; Expression *getElement(d_size_t i); // use opIndex instead Expression *opIndex(d_size_t i); bool isBool(bool result); @@ -427,7 +427,7 @@ class AssocArrayLiteralExp : public Expression Expressions *values; OwnedBy ownedByCtfe; - bool equals(RootObject *o); + bool equals(RootObject *o) const; Expression *syntaxCopy(); bool isBool(bool result); @@ -464,7 +464,7 @@ class StructLiteralExp : public Expression OwnedBy ownedByCtfe; static StructLiteralExp *create(Loc loc, StructDeclaration *sd, void *elements, Type *stype = NULL); - bool equals(RootObject *o); + bool equals(RootObject *o) const; Expression *syntaxCopy(); Expression *getField(Type *type, unsigned offset); int getFieldIndex(Type *type, unsigned offset); @@ -571,7 +571,7 @@ class VarExp : public SymbolExp { public: static VarExp *create(Loc loc, Declaration *var, bool hasOverloads = true); - bool equals(RootObject *o); + bool equals(RootObject *o) const; int checkModifiable(Scope *sc, int flag); bool isLvalue(); Expression *toLvalue(Scope *sc, Expression *e); @@ -601,9 +601,9 @@ class FuncExp : public Expression TemplateDeclaration *td; TOK tok; - bool equals(RootObject *o); + bool equals(RootObject *o) const; Expression *syntaxCopy(); - const char *toChars(); + const char *toChars() const; bool checkType(); bool checkValue(); diff --git a/src/dmd/identifier.h b/src/dmd/identifier.h index 146b833e088d..1d28f9759b6f 100644 --- a/src/dmd/identifier.h +++ b/src/dmd/identifier.h @@ -22,10 +22,10 @@ class Identifier : public RootObject public: static Identifier* anonymous(); static Identifier* create(const char *string); - bool equals(RootObject *o); - const char *toChars(); + bool equals(RootObject *o) const; + const char *toChars() const; int getValue() const; - const char *toHChars2(); + const char *toHChars2() const; DYNCAST dyncast() const; static Identifier *generateId(const char *prefix); diff --git a/src/dmd/init.h b/src/dmd/init.h index d26a828aba51..9047a0e9acf2 100644 --- a/src/dmd/init.h +++ b/src/dmd/init.h @@ -32,7 +32,7 @@ class Initializer : public ASTNode Loc loc; unsigned char kind; - const char *toChars(); + const char *toChars() const; ErrorInitializer *isErrorInitializer(); VoidInitializer *isVoidInitializer(); diff --git a/src/dmd/mtype.h b/src/dmd/mtype.h index bf8a9e0b27f7..7a3e5972b331 100644 --- a/src/dmd/mtype.h +++ b/src/dmd/mtype.h @@ -228,12 +228,12 @@ class Type : public ASTNode virtual const char *kind(); Type *copy() const; virtual Type *syntaxCopy(); - bool equals(RootObject *o); + bool equals(RootObject *o) const; bool equivalent(Type *t); // kludge for template.isType() DYNCAST dyncast() const { return DYNCAST_TYPE; } int covariant(Type *t, StorageClass *pstc = NULL, bool fix17349 = true); - const char *toChars(); + const char *toChars() const; char *toPrettyChars(bool QualifyTypes = false); static void _init(); @@ -570,7 +570,7 @@ class Parameter : public ASTNode static size_t dim(Parameters *parameters); static Parameter *getNth(Parameters *parameters, d_size_t nth, d_size_t *pn = NULL); - const char *toChars(); + const char *toChars() const; bool isCovariant(bool returnByRef, const Parameter *p) const; }; @@ -830,7 +830,7 @@ class TypeTuple : public Type static TypeTuple *create(Type *t1, Type *t2); const char *kind(); Type *syntaxCopy(); - bool equals(RootObject *o); + bool equals(RootObject *o) const; void accept(Visitor *v) { v->visit(this); } }; diff --git a/src/dmd/root/array.h b/src/dmd/root/array.h index 17953121c410..13c6280849f6 100644 --- a/src/dmd/root/array.h +++ b/src/dmd/root/array.h @@ -43,7 +43,7 @@ struct Array mem.xfree(data); } - char *toChars() + char *toChars() const { const char **buf = (const char **)mem.xmalloc(dim * sizeof(const char *)); d_size_t len = 2; diff --git a/src/dmd/statement.h b/src/dmd/statement.h index 52997e71246f..f52c69f437ee 100644 --- a/src/dmd/statement.h +++ b/src/dmd/statement.h @@ -114,7 +114,7 @@ class Statement : public ASTNode virtual Statement *syntaxCopy(); - const char *toChars(); + const char *toChars() const; void error(const char *format, ...); void warning(const char *format, ...); diff --git a/src/dmd/template.h b/src/dmd/template.h index 2886908ff65a..c7a32330b87a 100644 --- a/src/dmd/template.h +++ b/src/dmd/template.h @@ -36,7 +36,7 @@ class Tuple : public RootObject // kludge for template.isType() DYNCAST dyncast() const { return DYNCAST_TUPLE; } - const char *toChars() { return objects.toChars(); } + const char *toChars() const { return objects.toChars(); } }; struct TemplatePrevious @@ -75,7 +75,7 @@ class TemplateDeclaration : public ScopeDsymbol bool overloadInsert(Dsymbol *s); bool hasStaticCtorOrDtor(); const char *kind() const; - const char *toChars(); + const char *toChars() const; Prot prot(); @@ -275,7 +275,7 @@ class TemplateInstance : public ScopeDsymbol Dsymbol *toAlias(); // resolve real symbol const char *kind() const; bool oneMember(Dsymbol **ps, Identifier *ident); - const char *toChars(); + const char *toChars() const; const char* toPrettyCharsHelper(); void printInstantiationTrace(); Identifier *getIdent(); @@ -298,7 +298,7 @@ class TemplateMixin : public TemplateInstance int apply(Dsymbol_apply_ft_t fp, void *param); bool hasPointers(); void setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion); - const char *toChars(); + const char *toChars() const; TemplateMixin *isTemplateMixin() { return this; } void accept(Visitor *v) { v->visit(this); } diff --git a/src/dmd/version.h b/src/dmd/version.h index ea1b89088f66..6e2466854291 100644 --- a/src/dmd/version.h +++ b/src/dmd/version.h @@ -19,7 +19,7 @@ class DebugSymbol : public Dsymbol Dsymbol *syntaxCopy(Dsymbol *); - const char *toChars(); + const char *toChars() const; void addMember(Scope *sc, ScopeDsymbol *sds); const char *kind() const; void accept(Visitor *v) { v->visit(this); } @@ -32,7 +32,7 @@ class VersionSymbol : public Dsymbol Dsymbol *syntaxCopy(Dsymbol *); - const char *toChars(); + const char *toChars() const; void addMember(Scope *sc, ScopeDsymbol *sds); const char *kind() const; void accept(Visitor *v) { v->visit(this); } From c3f026862f221fbb7a02864f984b9f3d86cb6184 Mon Sep 17 00:00:00 2001 From: Eduard Staniloiu Date: Thu, 15 Aug 2019 15:05:35 +0300 Subject: [PATCH 4/5] Address feedback --- src/dmd/declaration.d | 5 +++-- src/dmd/declaration.h | 4 ++-- src/dmd/doc.d | 5 +++++ src/dmd/dsymbol.d | 2 +- src/dmd/dsymbol.h | 2 +- src/dmd/dtemplate.d | 2 +- src/dmd/expression.d | 45 +++++++++++++++++++-------------------- src/dmd/expression.h | 22 +++++++++---------- src/dmd/func.d | 9 ++++---- src/dmd/hdrgen.d | 17 ++++++--------- src/dmd/identifier.h | 2 +- src/dmd/mtype.d | 4 ++-- src/dmd/mtype.h | 4 ++-- src/dmd/root/array.d | 10 +-------- src/dmd/root/object.h | 4 ++-- src/dmd/root/rootobject.d | 2 +- 16 files changed, 66 insertions(+), 73 deletions(-) diff --git a/src/dmd/declaration.d b/src/dmd/declaration.d index c899cdf44df5..5f4d3f4bc9cf 100644 --- a/src/dmd/declaration.d +++ b/src/dmd/declaration.d @@ -980,7 +980,7 @@ extern (C++) final class OverDeclaration : Declaration return "overload alias"; // todo } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -1000,7 +1000,8 @@ extern (C++) final class OverDeclaration : Declaration return true; if (auto fd = s.isFuncDeclaration()) { - return fd.isUnique(); + // isUnique is not callable using a const object + return (cast() fd).isUnique(); } if (auto td = s.isTemplateDeclaration()) { diff --git a/src/dmd/declaration.h b/src/dmd/declaration.h index f3bc01aa9709..61cb23d659f2 100644 --- a/src/dmd/declaration.h +++ b/src/dmd/declaration.h @@ -193,7 +193,7 @@ class OverDeclaration : public Declaration bool hasOverloads; const char *kind() const; - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; bool overloadInsert(Dsymbol *s); Dsymbol *toAlias(); @@ -561,7 +561,7 @@ class FuncDeclaration : public Declaration Dsymbol *syntaxCopy(Dsymbol *); bool functionSemantic(); bool functionSemantic3(); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; int overrides(FuncDeclaration *fd); int findVtblIndex(Dsymbols *vtbl, int dim, bool fix17349 = true); diff --git a/src/dmd/doc.d b/src/dmd/doc.d index cfc46ddef5f7..4eefe803167e 100644 --- a/src/dmd/doc.d +++ b/src/dmd/doc.d @@ -98,6 +98,11 @@ private class Section size_t bodylen; int nooutput; + override string toString() const + { + assert(0); + } + void write(Loc loc, DocComment* dc, Scope* sc, Dsymbols* a, OutBuffer* buf) { assert(a.dim); diff --git a/src/dmd/dsymbol.d b/src/dmd/dsymbol.d index 379a7b678c09..a4b99d8ac91c 100644 --- a/src/dmd/dsymbol.d +++ b/src/dmd/dsymbol.d @@ -296,7 +296,7 @@ extern (C++) class Dsymbol : ASTNode return getLoc().toChars(); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; diff --git a/src/dmd/dsymbol.h b/src/dmd/dsymbol.h index c2cfa0831623..942c4558843a 100644 --- a/src/dmd/dsymbol.h +++ b/src/dmd/dsymbol.h @@ -167,7 +167,7 @@ class Dsymbol : public ASTNode virtual const char *toPrettyCharsHelper(); // helper to print fully qualified (template) arguments Loc getLoc(); const char *locToChars(); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; virtual bool isAnonymous(); void error(const Loc &loc, const char *format, ...); void error(const char *format, ...); diff --git a/src/dmd/dtemplate.d b/src/dmd/dtemplate.d index f36e0cc02fbe..a9f7ade85a84 100644 --- a/src/dmd/dtemplate.d +++ b/src/dmd/dtemplate.d @@ -5267,7 +5267,7 @@ extern (C++) class TemplateParameter : ASTNode abstract bool hasDefaultArg(); - override const(char)* toChars() const + override const(char)* toChars() const { return this.ident.toChars(); } diff --git a/src/dmd/expression.d b/src/dmd/expression.d index 8f7b44a13be8..424d342c084e 100644 --- a/src/dmd/expression.d +++ b/src/dmd/expression.d @@ -1679,7 +1679,7 @@ extern (C++) final class IntegerExp : Expression emplaceExp!(IntegerExp)(pue, loc, value, type); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -1923,7 +1923,7 @@ extern (C++) final class RealExp : Expression emplaceExp!(RealExp)(pue, loc, value, type); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -1998,7 +1998,7 @@ extern (C++) final class ComplexExp : Expression emplaceExp!(ComplexExp)(pue, loc, value, type); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -2214,7 +2214,7 @@ extern (C++) final class NullExp : Expression this.type = type; } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (auto e = o.isExpression()) { @@ -2311,7 +2311,7 @@ extern (C++) final class StringExp : Expression emplaceExp!(StringExp)(pue, loc, string, len); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { //printf("StringExp::equals('%s') %s\n", o.toChars(), toChars()); if (auto e = o.isExpression()) @@ -2694,7 +2694,7 @@ extern (C++) final class TupleExp : Expression return new TupleExp(loc, e0 ? e0.syntaxCopy() : null, arraySyntaxCopy(exps)); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -2707,7 +2707,7 @@ extern (C++) final class TupleExp : Expression return false; foreach (i, e1; *exps) { - Expression e2 = (*te.exps)[i]; + auto e2 = (*te.exps)[i]; if (!e1.equals(e2)) return false; } @@ -2781,11 +2781,11 @@ extern (C++) final class ArrayLiteralExp : Expression arraySyntaxCopy(elements)); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; - Expression e = o.isExpression(); + auto e = o.isExpression(); if (!e) return false; if (auto ae = e.isArrayLiteralExp()) @@ -2799,12 +2799,11 @@ extern (C++) final class ArrayLiteralExp : Expression Expressions* unqualElements = cast(Expressions*) elements; foreach (i, e1; *unqualElements) { - Expression e2 = (*ae.elements)[i]; - if (!e1) - e1 = cast(Expression) basis; - if (!e2) - e2 = ae.basis; - if (e1 != e2 && (!e1 || !e2 || !e1.equals(e2))) + auto e2 = (*ae.elements)[i]; + auto e1x = e1 ? e1 : basis; + auto e2x = e2 ? e2 : ae.basis; + + if (e1x != e2x && (!e1x || !e2x || !e1x.equals(e2x))) return false; } return true; @@ -2909,7 +2908,7 @@ extern (C++) final class AssocArrayLiteralExp : Expression this.values = values; } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -3010,11 +3009,11 @@ extern (C++) final class StructLiteralExp : Expression return new StructLiteralExp(loc, sd, cast(Expressions*)elements, stype); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; - Expression e = o.isExpression(); + auto e = o.isExpression(); if (!e) return false; if (auto se = e.isStructLiteralExp()) @@ -3025,7 +3024,7 @@ extern (C++) final class StructLiteralExp : Expression return false; foreach (i, e1; *elements) { - Expression e2 = (*se.elements)[i]; + auto e2 = (*se.elements)[i]; if (e1 != e2 && (!e1 || !e2 || !e1.equals(e2))) return false; } @@ -3444,7 +3443,7 @@ extern (C++) final class VarExp : SymbolExp return new VarExp(loc, var, hasOverloads); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -3577,7 +3576,7 @@ extern (C++) final class FuncExp : Expression assert(fd.fbody); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -4408,7 +4407,7 @@ extern (C++) final class CompileExp : Expression return new CompileExp(loc, arraySyntaxCopy(exps)); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { if (this == o) return true; @@ -4421,7 +4420,7 @@ extern (C++) final class CompileExp : Expression return false; foreach (i, e1; *exps) { - Expression e2 = (*ce.exps)[i]; + auto e2 = (*ce.exps)[i]; if (e1 != e2 && (!e1 || !e2 || !e1.equals(e2))) return false; } diff --git a/src/dmd/expression.h b/src/dmd/expression.h index fa94411567a7..4c046e44c273 100644 --- a/src/dmd/expression.h +++ b/src/dmd/expression.h @@ -234,7 +234,7 @@ class IntegerExp : public Expression static IntegerExp *create(Loc loc, dinteger_t value, Type *type); static void emplace(UnionExp *pue, Loc loc, dinteger_t value, Type *type); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; dinteger_t toInteger(); real_t toReal(); real_t toImaginary(); @@ -264,7 +264,7 @@ class RealExp : public Expression static RealExp *create(Loc loc, real_t value, Type *type); static void emplace(UnionExp *pue, Loc loc, real_t value, Type *type); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; dinteger_t toInteger(); uinteger_t toUInteger(); real_t toReal(); @@ -281,7 +281,7 @@ class ComplexExp : public Expression static ComplexExp *create(Loc loc, complex_t value, Type *type); static void emplace(UnionExp *pue, Loc loc, complex_t value, Type *type); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; dinteger_t toInteger(); uinteger_t toUInteger(); real_t toReal(); @@ -344,7 +344,7 @@ class NullExp : public Expression public: unsigned char committed; // !=0 if type is committed - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; bool isBool(bool result); StringExp *toStringExp(); void accept(Visitor *v) { v->visit(this); } @@ -364,7 +364,7 @@ class StringExp : public Expression static StringExp *create(Loc loc, void *s, size_t len); static void emplace(UnionExp *pue, Loc loc, char *s); static void emplace(UnionExp *pue, Loc loc, void *s, size_t len); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; StringExp *toStringExp(); StringExp *toUTF8(Scope *sc); bool isBool(bool result); @@ -396,7 +396,7 @@ class TupleExp : public Expression static TupleExp *create(Loc loc, Expressions *exps); TupleExp *toTupleExp(); Expression *syntaxCopy(); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; void accept(Visitor *v) { v->visit(this); } }; @@ -411,7 +411,7 @@ class ArrayLiteralExp : public Expression static ArrayLiteralExp *create(Loc loc, Expressions *elements); static void emplace(UnionExp *pue, Loc loc, Expressions *elements); Expression *syntaxCopy(); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; Expression *getElement(d_size_t i); // use opIndex instead Expression *opIndex(d_size_t i); bool isBool(bool result); @@ -427,7 +427,7 @@ class AssocArrayLiteralExp : public Expression Expressions *values; OwnedBy ownedByCtfe; - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; Expression *syntaxCopy(); bool isBool(bool result); @@ -464,7 +464,7 @@ class StructLiteralExp : public Expression OwnedBy ownedByCtfe; static StructLiteralExp *create(Loc loc, StructDeclaration *sd, void *elements, Type *stype = NULL); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; Expression *syntaxCopy(); Expression *getField(Type *type, unsigned offset); int getFieldIndex(Type *type, unsigned offset); @@ -571,7 +571,7 @@ class VarExp : public SymbolExp { public: static VarExp *create(Loc loc, Declaration *var, bool hasOverloads = true); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; int checkModifiable(Scope *sc, int flag); bool isLvalue(); Expression *toLvalue(Scope *sc, Expression *e); @@ -601,7 +601,7 @@ class FuncExp : public Expression TemplateDeclaration *td; TOK tok; - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; Expression *syntaxCopy(); const char *toChars() const; bool checkType(); diff --git a/src/dmd/func.d b/src/dmd/func.d index d889bce622ce..84425ae3189c 100644 --- a/src/dmd/func.d +++ b/src/dmd/func.d @@ -558,14 +558,14 @@ extern (C++) class FuncDeclaration : Declaration return HiddenParameters.init; } - override final bool equals(RootObject o) const + override final bool equals(const RootObject o) const { if (this == o) return true; if (auto s = isDsymbol(o)) { - auto fd1 = cast(FuncDeclaration) this; + auto fd1 = this; auto fd2 = s.isFuncDeclaration(); if (!fd2) return false; @@ -582,11 +582,12 @@ extern (C++) class FuncDeclaration : Declaration } bool b1 = fa1 !is null; - if (b1 && faf1.isUnique() && !fa1.hasOverloads) + // isUnique is not callable using a const object + if (b1 && (cast() faf1).isUnique() && !fa1.hasOverloads) b1 = false; bool b2 = fa2 !is null; - if (b2 && faf2.isUnique() && !fa2.hasOverloads) + if (b2 && (cast() faf2).isUnique() && !fa2.hasOverloads) b2 = false; if (b1 != b2) diff --git a/src/dmd/hdrgen.d b/src/dmd/hdrgen.d index d7c57e871262..affa86e9a876 100644 --- a/src/dmd/hdrgen.d +++ b/src/dmd/hdrgen.d @@ -2632,14 +2632,12 @@ public: void toCBuffer(const Statement s, OutBuffer* buf, HdrGenState* hgs) { scope v = new StatementPrettyPrintVisitor(buf, hgs); - Statement unqualS = cast(Statement) s; - unqualS.accept(v); + (cast() s).accept(v); } void toCBuffer(const Type t, OutBuffer* buf, const Identifier ident, HdrGenState* hgs) { - Type unqualT = cast(Type) t; - typeToBuffer(unqualT, ident, buf, hgs); + typeToBuffer(cast() t, ident, buf, hgs); } void toCBuffer(Dsymbol s, OutBuffer* buf, HdrGenState* hgs) @@ -2654,13 +2652,12 @@ void toCBufferInstance(const TemplateInstance ti, OutBuffer* buf, bool qualifyTy HdrGenState hgs; hgs.fullQual = qualifyTypes; scope v = new DsymbolPrettyPrintVisitor(buf, &hgs); - v.visit(cast(TemplateInstance) ti); + v.visit(cast() ti); } void toCBuffer(const Initializer iz, OutBuffer* buf, HdrGenState* hgs) { - Initializer unqualIz = cast(Initializer) iz; - initializerToBuffer(unqualIz, buf, hgs); + initializerToBuffer(cast() iz, buf, hgs); } bool stcToBuffer(OutBuffer* buf, StorageClass stc) @@ -2891,8 +2888,7 @@ void functionToBufferWithIdent(TypeFunction tf, OutBuffer* buf, const(char)* ide void toCBuffer(const Expression e, OutBuffer* buf, HdrGenState* hgs) { scope v = new ExpressionPrettyPrintVisitor(buf, hgs); - Expression unqualE = cast(Expression) e; - unqualE.accept(v); + (cast() e).accept(v); } /************************************************** @@ -2914,8 +2910,7 @@ void argExpTypesToCBuffer(OutBuffer* buf, Expressions* arguments) void toCBuffer(const TemplateParameter tp, OutBuffer* buf, HdrGenState* hgs) { scope v = new TemplateParameterPrettyPrintVisitor(buf, hgs); - TemplateParameter unqualTP = cast(TemplateParameter) tp; - unqualTP.accept(v); + (cast() tp).accept(v); } void arrayObjectsToBuffer(OutBuffer* buf, Objects* objects) diff --git a/src/dmd/identifier.h b/src/dmd/identifier.h index 1d28f9759b6f..a395f7f6db5a 100644 --- a/src/dmd/identifier.h +++ b/src/dmd/identifier.h @@ -22,7 +22,7 @@ class Identifier : public RootObject public: static Identifier* anonymous(); static Identifier* create(const char *string); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; const char *toChars() const; int getValue() const; const char *toHChars2() const; diff --git a/src/dmd/mtype.d b/src/dmd/mtype.d index 0983745392d6..c9c4a7f2a8ad 100644 --- a/src/dmd/mtype.d +++ b/src/dmd/mtype.d @@ -517,7 +517,7 @@ extern (C++) abstract class Type : ASTNode assert(0); } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { Type t = cast(Type)o; //printf("Type::equals(%s, %s)\n", toChars(), t.toChars()); @@ -6206,7 +6206,7 @@ extern (C++) final class TypeTuple : Type return t; } - override bool equals(RootObject o) const + override bool equals(const RootObject o) const { Type t = cast(Type)o; //printf("TypeTuple::equals(%s, %s)\n", toChars(), t.toChars()); diff --git a/src/dmd/mtype.h b/src/dmd/mtype.h index 7a3e5972b331..8e10a62c3e73 100644 --- a/src/dmd/mtype.h +++ b/src/dmd/mtype.h @@ -228,7 +228,7 @@ class Type : public ASTNode virtual const char *kind(); Type *copy() const; virtual Type *syntaxCopy(); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; bool equivalent(Type *t); // kludge for template.isType() DYNCAST dyncast() const { return DYNCAST_TYPE; } @@ -830,7 +830,7 @@ class TypeTuple : public Type static TypeTuple *create(Type *t1, Type *t2); const char *kind(); Type *syntaxCopy(); - bool equals(RootObject *o) const; + bool equals(const RootObject *o) const; void accept(Visitor *v) { v->visit(this); } }; diff --git a/src/dmd/root/array.d b/src/dmd/root/array.d index 5f8f37d21259..193591086c8c 100644 --- a/src/dmd/root/array.d +++ b/src/dmd/root/array.d @@ -53,15 +53,7 @@ public: size_t len = 2; // [ and ] foreach (u; 0 .. length) { - static if (is(typeof(this) == const(A), A) && is (A == Array!UnqualT, UnqualT)) - { - UnqualT* unqualData = cast(UnqualT*) data; - buf[u] = unqualData[u].toString(); - } - else - { - buf[u] = data[u].toString(); - } + buf[u] = data[u].toString(); len += buf[u].length + 1; //length + ',' or null terminator } char[] str = (cast(char*)mem.xmalloc(len))[0..len]; diff --git a/src/dmd/root/object.h b/src/dmd/root/object.h index eb51bac7d39d..d499c0a972d0 100644 --- a/src/dmd/root/object.h +++ b/src/dmd/root/object.h @@ -39,12 +39,12 @@ class RootObject public: RootObject() { } - virtual bool equals(RootObject *o); + virtual bool equals(const RootObject *o) const; /** * Pretty-print an Object. Useful for debugging the old-fashioned way. */ - virtual const char *toChars(); + virtual const char *toChars() const; /// This function is `extern(D)` and should not be called from C++, /// as the ABI does not match on some platforms virtual DString toString(); diff --git a/src/dmd/root/rootobject.d b/src/dmd/root/rootobject.d index 7d74d3535db0..bb84a04b32da 100644 --- a/src/dmd/root/rootobject.d +++ b/src/dmd/root/rootobject.d @@ -42,7 +42,7 @@ extern (C++) class RootObject { } - bool equals(RootObject o) const + bool equals(const RootObject o) const { return o is this; } From bd34cc5d45b91514b25c3bd0e19d2091be54ec1b Mon Sep 17 00:00:00 2001 From: Eduard Staniloiu Date: Mon, 19 Aug 2019 17:49:11 +0300 Subject: [PATCH 5/5] qf --- src/dmd/declaration.d | 3 +-- src/dmd/expression.d | 4 ++-- src/dmd/func.d | 9 ++++----- src/dmd/mtype.d | 1 + src/dmd/root/array.d | 2 +- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/dmd/declaration.d b/src/dmd/declaration.d index 5f4d3f4bc9cf..dc36c41ee50e 100644 --- a/src/dmd/declaration.d +++ b/src/dmd/declaration.d @@ -1000,8 +1000,7 @@ extern (C++) final class OverDeclaration : Declaration return true; if (auto fd = s.isFuncDeclaration()) { - // isUnique is not callable using a const object - return (cast() fd).isUnique(); + return fd.isUnique(); } if (auto td = s.isTemplateDeclaration()) { diff --git a/src/dmd/expression.d b/src/dmd/expression.d index 424d342c084e..64f27df02174 100644 --- a/src/dmd/expression.d +++ b/src/dmd/expression.d @@ -2796,8 +2796,8 @@ extern (C++) final class ArrayLiteralExp : Expression { return false; } - Expressions* unqualElements = cast(Expressions*) elements; - foreach (i, e1; *unqualElements) + + foreach (i, e1; *elements) { auto e2 = (*ae.elements)[i]; auto e1x = e1 ? e1 : basis; diff --git a/src/dmd/func.d b/src/dmd/func.d index 84425ae3189c..cea1b8c05acb 100644 --- a/src/dmd/func.d +++ b/src/dmd/func.d @@ -582,12 +582,11 @@ extern (C++) class FuncDeclaration : Declaration } bool b1 = fa1 !is null; - // isUnique is not callable using a const object - if (b1 && (cast() faf1).isUnique() && !fa1.hasOverloads) + if (b1 && faf1.isUnique() && !fa1.hasOverloads) b1 = false; bool b2 = fa2 !is null; - if (b2 && (cast() faf2).isUnique() && !fa2.hasOverloads) + if (b2 && faf2.isUnique() && !fa2.hasOverloads) b2 = false; if (b1 != b2) @@ -1744,10 +1743,10 @@ extern (C++) class FuncDeclaration : Declaration * Returns: * true if there are no overloads of this function */ - final bool isUnique() + final bool isUnique() const { bool result = false; - overloadApply(this, (Dsymbol s) + overloadApply(cast() this, (Dsymbol s) { auto f = s.isFuncDeclaration(); if (!f) diff --git a/src/dmd/mtype.d b/src/dmd/mtype.d index c9c4a7f2a8ad..9c267534bd20 100644 --- a/src/dmd/mtype.d +++ b/src/dmd/mtype.d @@ -2387,6 +2387,7 @@ extern (C++) abstract class Type : ASTNode if (!mod) return this; Type unqualThis = cast(Type) this; + // `mutableOf` needs a mutable `this` only for caching return cast(inout(Type)) unqualThis.mutableOf(); } diff --git a/src/dmd/root/array.d b/src/dmd/root/array.d index 193591086c8c..760e19020fff 100644 --- a/src/dmd/root/array.d +++ b/src/dmd/root/array.d @@ -249,7 +249,7 @@ unittest static struct S { int s = -1; - string toString() + string toString() const { return "S"; }