2 * Semantic analysis of template parameters.
4 * Copyright: Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/templateparamsem.d, _templateparamsem.d)
8 * Documentation: https://dlang.org/phobos/dmd_templateparamsem.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/templateparamsem.d
12 module dmd.templateparamsem;
14 import dmd.arraytypes;
20 import dmd.expression;
21 import dmd.expressionsem;
22 import dmd.root.rootobject;
27 /************************************************
28 * Performs semantic on TemplateParameter AST nodes.
31 * tp = element of `parameters` to be semantically analyzed
33 * parameters = array of `TemplateParameters` supplied to the `TemplateDeclaration`
37 extern(C++) bool tpsemantic(TemplateParameter tp, Scope* sc, TemplateParameters* parameters)
39 scope v = new TemplateParameterSemanticVisitor(sc, parameters);
45 private extern (C++) final class TemplateParameterSemanticVisitor : Visitor
47 alias visit = Visitor.visit;
50 TemplateParameters* parameters;
53 this(Scope* sc, TemplateParameters* parameters) scope
56 this.parameters = parameters;
59 override void visit(TemplateTypeParameter ttp)
61 //printf("TemplateTypeParameter.semantic('%s')\n", ident.toChars());
62 if (ttp.specType && !reliesOnTident(ttp.specType, parameters))
64 ttp.specType = ttp.specType.typeSemantic(ttp.loc, sc);
68 // Don't do semantic() until instantiation
71 ttp.defaultType = ttp.defaultType.typeSemantic(ttp.loc, sc);
74 result = !(ttp.specType && isError(ttp.specType));
77 override void visit(TemplateThisParameter ttp)
81 if (!sc.getStructClassScope())
82 error(ttp.loc, "cannot use `this` outside an aggregate type");
83 visit(cast(TemplateTypeParameter)ttp);
86 override void visit(TemplateValueParameter tvp)
88 tvp.valType = tvp.valType.typeSemantic(tvp.loc, sc);
91 // defer semantic analysis to arg match
94 Expression e = tvp.specValue;
98 e = e.implicitCastTo(sc, tvp.valType);
99 e = e.ctfeInterpret();
100 if (e.op == EXP.int64 || e.op == EXP.float64 ||
101 e.op == EXP.complex80 || e.op == EXP.null_ || e.op == EXP.string_)
105 if (tvp.defaultValue)
107 Expression e = defaultValue;
111 e = e.implicitCastTo(sc, tvp.valType);
112 e = e.ctfeInterpret();
113 if (e.op == EXP.int64)
114 tvp.defaultValue = e;
117 result = !isError(tvp.valType);
120 override void visit(TemplateAliasParameter tap)
122 if (tap.specType && !reliesOnTident(tap.specType, parameters))
124 tap.specType = tap.specType.typeSemantic(tap.loc, sc);
126 tap.specAlias = aliasParameterSemantic(tap.loc, sc, tap.specAlias, parameters);
129 // Don't do semantic() until instantiation
130 if (tap.defaultAlias)
131 tap.defaultAlias = tap.defaultAlias.semantic(tap.loc, sc);
133 result = !(tap.specType && isError(tap.specType)) && !(tap.specAlias && isError(tap.specAlias));
136 override void visit(TemplateTupleParameter ttp)
142 /***********************************************
143 * Support function for performing semantic analysis on `TemplateAliasParameter`.
146 * loc = location (for error messages)
148 * o = object to run semantic() on, the `TemplateAliasParameter`s `specAlias` or `defaultAlias`
149 * parameters = array of `TemplateParameters` supplied to the `TemplateDeclaration`
151 * object resulting from running `semantic` on `o`
153 RootObject aliasParameterSemantic(Loc loc, Scope* sc, RootObject o, TemplateParameters* parameters)
158 Expression ea = isExpression(o);
162 ea = ea.expressionSemantic(sc);
164 return ea.ctfeInterpret();
167 if (ta && (!parameters || !reliesOnTident(ta, parameters)))
169 Dsymbol s = ta.toDsymbol(sc);
172 else if (TypeInstance ti = ta.isTypeInstance())
175 const errors = global.errors;
176 ta.resolve(loc, sc, ea, t, s);
177 // if we had an error evaluating the symbol, suppress further errors
178 if (!t && errors != global.errors)
180 // We might have something that looks like a type
181 // but is actually an expression or a dsymbol
182 // see https://issues.dlang.org/show_bug.cgi?id=16472
184 return t.typeSemantic(loc, sc);
195 return ta.typeSemantic(loc, sc);