| 1 |
|
| 2 |
/* Compiler implementation of the D programming language |
| 3 |
* Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 4 |
* written by Walter Bright |
| 5 |
* https://www.digitalmars.com |
| 6 |
* Distributed under the Boost Software License, Version 1.0. |
| 7 |
* https://www.boost.org/LICENSE_1_0.txt |
| 8 |
* https://github.com/dlang/dmd/blob/master/src/dmd/template.h |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "arraytypes.h" |
| 14 |
#include "dsymbol.h" |
| 15 |
#include "expression.h" |
| 16 |
|
| 17 |
class Identifier; |
| 18 |
class TemplateInstance; |
| 19 |
class TemplateParameter; |
| 20 |
class TemplateTypeParameter; |
| 21 |
class TemplateThisParameter; |
| 22 |
class TemplateValueParameter; |
| 23 |
class TemplateAliasParameter; |
| 24 |
class TemplateTupleParameter; |
| 25 |
class Type; |
| 26 |
class TypeQualified; |
| 27 |
struct Scope; |
| 28 |
class Expression; |
| 29 |
class FuncDeclaration; |
| 30 |
class Parameter; |
| 31 |
|
| 32 |
class Tuple final : public RootObject |
| 33 |
{ |
| 34 |
public: |
| 35 |
Objects objects; |
| 36 |
|
| 37 |
// kludge for template.isType() |
| 38 |
DYNCAST dyncast() const override { return DYNCAST_TUPLE; } |
| 39 |
|
| 40 |
const char *toChars() const override; |
| 41 |
}; |
| 42 |
|
| 43 |
struct TemplatePrevious |
| 44 |
{ |
| 45 |
TemplatePrevious *prev; |
| 46 |
Scope *sc; |
| 47 |
Objects *dedargs; |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
class TemplateDeclaration final : public ScopeDsymbol |
| 52 |
{ |
| 53 |
public: |
| 54 |
TemplateParameters *parameters; // array of TemplateParameter's |
| 55 |
|
| 56 |
TemplateParameters *origParameters; // originals for Ddoc |
| 57 |
Expression *constraint; |
| 58 |
|
| 59 |
// Hash table to look up TemplateInstance's of this TemplateDeclaration |
| 60 |
void *instances; |
| 61 |
|
| 62 |
TemplateDeclaration *overnext; // next overloaded TemplateDeclaration |
| 63 |
TemplateDeclaration *overroot; // first in overnext list |
| 64 |
FuncDeclaration *funcroot; // first function in unified overload list |
| 65 |
|
| 66 |
Dsymbol *onemember; // if !=NULL then one member of this template |
| 67 |
|
| 68 |
d_bool literal; // this template declaration is a literal |
| 69 |
d_bool ismixin; // template declaration is only to be used as a mixin |
| 70 |
d_bool isstatic; // this is static template declaration |
| 71 |
d_bool isTrivialAliasSeq; // matches `template AliasSeq(T...) { alias AliasSeq = T; } |
| 72 |
d_bool isTrivialAlias; // matches pattern `template Alias(T) { alias Alias = qualifiers(T); }` |
| 73 |
d_bool deprecated_; // this template declaration is deprecated |
| 74 |
Visibility visibility; |
| 75 |
|
| 76 |
TemplatePrevious *previous; // threaded list of previous instantiation attempts on stack |
| 77 |
|
| 78 |
TemplateDeclaration *syntaxCopy(Dsymbol *) override; |
| 79 |
bool overloadInsert(Dsymbol *s) override; |
| 80 |
const char *kind() const override; |
| 81 |
|
| 82 |
Visibility visible() override; |
| 83 |
|
| 84 |
|
| 85 |
bool isDeprecated() const override; |
| 86 |
bool isOverloadable() const override; |
| 87 |
|
| 88 |
void accept(Visitor *v) override { v->visit(this); } |
| 89 |
}; |
| 90 |
|
| 91 |
/* For type-parameter: |
| 92 |
* template Foo(ident) // specType is set to NULL |
| 93 |
* template Foo(ident : specType) |
| 94 |
* For value-parameter: |
| 95 |
* template Foo(valType ident) // specValue is set to NULL |
| 96 |
* template Foo(valType ident : specValue) |
| 97 |
* For alias-parameter: |
| 98 |
* template Foo(alias ident) |
| 99 |
* For this-parameter: |
| 100 |
* template Foo(this ident) |
| 101 |
*/ |
| 102 |
class TemplateParameter : public ASTNode |
| 103 |
{ |
| 104 |
public: |
| 105 |
Loc loc; |
| 106 |
Identifier *ident; |
| 107 |
|
| 108 |
/* True if this is a part of precedent parameter specialization pattern. |
| 109 |
* |
| 110 |
* template A(T : X!TL, alias X, TL...) {} |
| 111 |
* // X and TL are dependent template parameter |
| 112 |
* |
| 113 |
* A dependent template parameter should return MATCHexact in matchArg() |
| 114 |
* to respect the match level of the corresponding precedent parameter. |
| 115 |
*/ |
| 116 |
d_bool dependent; |
| 117 |
|
| 118 |
virtual TemplateTypeParameter *isTemplateTypeParameter(); |
| 119 |
virtual TemplateValueParameter *isTemplateValueParameter(); |
| 120 |
virtual TemplateAliasParameter *isTemplateAliasParameter(); |
| 121 |
virtual TemplateThisParameter *isTemplateThisParameter(); |
| 122 |
virtual TemplateTupleParameter *isTemplateTupleParameter(); |
| 123 |
|
| 124 |
virtual TemplateParameter *syntaxCopy() = 0; |
| 125 |
virtual bool declareParameter(Scope *sc) = 0; |
| 126 |
virtual void print(RootObject *oarg, RootObject *oded) = 0; |
| 127 |
virtual RootObject *specialization() = 0; |
| 128 |
virtual RootObject *defaultArg(Loc instLoc, Scope *sc) = 0; |
| 129 |
virtual bool hasDefaultArg() = 0; |
| 130 |
|
| 131 |
DYNCAST dyncast() const override { return DYNCAST_TEMPLATEPARAMETER; } |
| 132 |
|
| 133 |
void accept(Visitor *v) override { v->visit(this); } |
| 134 |
}; |
| 135 |
|
| 136 |
/* Syntax: |
| 137 |
* ident : specType = defaultType |
| 138 |
*/ |
| 139 |
class TemplateTypeParameter : public TemplateParameter |
| 140 |
{ |
| 141 |
public: |
| 142 |
Type *specType; // type parameter: if !=NULL, this is the type specialization |
| 143 |
Type *defaultType; |
| 144 |
|
| 145 |
TemplateTypeParameter *isTemplateTypeParameter() override final; |
| 146 |
TemplateTypeParameter *syntaxCopy() override; |
| 147 |
bool declareParameter(Scope *sc) override final; |
| 148 |
void print(RootObject *oarg, RootObject *oded) override final; |
| 149 |
RootObject *specialization() override final; |
| 150 |
RootObject *defaultArg(Loc instLoc, Scope *sc) override final; |
| 151 |
bool hasDefaultArg() override final; |
| 152 |
void accept(Visitor *v) override { v->visit(this); } |
| 153 |
}; |
| 154 |
|
| 155 |
/* Syntax: |
| 156 |
* this ident : specType = defaultType |
| 157 |
*/ |
| 158 |
class TemplateThisParameter final : public TemplateTypeParameter |
| 159 |
{ |
| 160 |
public: |
| 161 |
TemplateThisParameter *isTemplateThisParameter() override; |
| 162 |
TemplateThisParameter *syntaxCopy() override; |
| 163 |
void accept(Visitor *v) override { v->visit(this); } |
| 164 |
}; |
| 165 |
|
| 166 |
/* Syntax: |
| 167 |
* valType ident : specValue = defaultValue |
| 168 |
*/ |
| 169 |
class TemplateValueParameter final : public TemplateParameter |
| 170 |
{ |
| 171 |
public: |
| 172 |
Type *valType; |
| 173 |
Expression *specValue; |
| 174 |
Expression *defaultValue; |
| 175 |
|
| 176 |
TemplateValueParameter *isTemplateValueParameter() override; |
| 177 |
TemplateValueParameter *syntaxCopy() override; |
| 178 |
bool declareParameter(Scope *sc) override; |
| 179 |
void print(RootObject *oarg, RootObject *oded) override; |
| 180 |
RootObject *specialization() override; |
| 181 |
RootObject *defaultArg(Loc instLoc, Scope *sc) override; |
| 182 |
bool hasDefaultArg() override; |
| 183 |
void accept(Visitor *v) override { v->visit(this); } |
| 184 |
}; |
| 185 |
|
| 186 |
/* Syntax: |
| 187 |
* specType ident : specAlias = defaultAlias |
| 188 |
*/ |
| 189 |
class TemplateAliasParameter final : public TemplateParameter |
| 190 |
{ |
| 191 |
public: |
| 192 |
Type *specType; |
| 193 |
RootObject *specAlias; |
| 194 |
RootObject *defaultAlias; |
| 195 |
|
| 196 |
TemplateAliasParameter *isTemplateAliasParameter() override; |
| 197 |
TemplateAliasParameter *syntaxCopy() override; |
| 198 |
bool declareParameter(Scope *sc) override; |
| 199 |
void print(RootObject *oarg, RootObject *oded) override; |
| 200 |
RootObject *specialization() override; |
| 201 |
RootObject *defaultArg(Loc instLoc, Scope *sc) override; |
| 202 |
bool hasDefaultArg() override; |
| 203 |
void accept(Visitor *v) override { v->visit(this); } |
| 204 |
}; |
| 205 |
|
| 206 |
/* Syntax: |
| 207 |
* ident ... |
| 208 |
*/ |
| 209 |
class TemplateTupleParameter final : public TemplateParameter |
| 210 |
{ |
| 211 |
public: |
| 212 |
TemplateTupleParameter *isTemplateTupleParameter() override; |
| 213 |
TemplateTupleParameter *syntaxCopy() override; |
| 214 |
bool declareParameter(Scope *sc) override; |
| 215 |
void print(RootObject *oarg, RootObject *oded) override; |
| 216 |
RootObject *specialization() override; |
| 217 |
RootObject *defaultArg(Loc instLoc, Scope *sc) override; |
| 218 |
bool hasDefaultArg() override; |
| 219 |
void accept(Visitor *v) override { v->visit(this); } |
| 220 |
}; |
| 221 |
|
| 222 |
/* Given: |
| 223 |
* foo!(args) => |
| 224 |
* name = foo |
| 225 |
* tiargs = args |
| 226 |
*/ |
| 227 |
class TemplateInstance : public ScopeDsymbol |
| 228 |
{ |
| 229 |
public: |
| 230 |
Identifier *name; |
| 231 |
|
| 232 |
// Array of Types/Expressions of template |
| 233 |
// instance arguments [int*, char, 10*10] |
| 234 |
Objects *tiargs; |
| 235 |
|
| 236 |
// Array of Types/Expressions corresponding |
| 237 |
// to TemplateDeclaration.parameters |
| 238 |
// [int, char, 100] |
| 239 |
Objects tdtypes; |
| 240 |
|
| 241 |
// Modules imported by this template instance |
| 242 |
Modules importedModules; |
| 243 |
|
| 244 |
Dsymbol *tempdecl; // referenced by foo.bar.abc |
| 245 |
Dsymbol *enclosing; // if referencing local symbols, this is the context |
| 246 |
Dsymbol *aliasdecl; // !=NULL if instance is an alias for its sole member |
| 247 |
TemplateInstance *inst; // refer to existing instance |
| 248 |
ScopeDsymbol *argsym; // argument symbol table |
| 249 |
hash_t hash; // cached result of toHash() |
| 250 |
Expressions *fargs; // for function template, these are the function arguments |
| 251 |
Identifiers *fnames; // for function template, argument names |
| 252 |
|
| 253 |
TemplateInstances* deferred; |
| 254 |
|
| 255 |
Module *memberOf; // if !null, then this TemplateInstance appears in memberOf.members[] |
| 256 |
|
| 257 |
// Used to determine the instance needs code generation. |
| 258 |
// Note that these are inaccurate until semantic analysis phase completed. |
| 259 |
TemplateInstance *tinst; // enclosing template instance |
| 260 |
TemplateInstance *tnext; // non-first instantiated instances |
| 261 |
Module *minst; // the top module that instantiated this instance |
| 262 |
|
| 263 |
private: |
| 264 |
unsigned short _nest; // for recursive pretty printing detection, 3 MSBs reserved for flags |
| 265 |
public: |
| 266 |
unsigned char inuse; // for recursive expansion detection |
| 267 |
|
| 268 |
TemplateInstance *syntaxCopy(Dsymbol *) override; |
| 269 |
Dsymbol *toAlias() override final; // resolve real symbol |
| 270 |
const char *kind() const override; |
| 271 |
const char* toPrettyCharsHelper() override final; |
| 272 |
Identifier *getIdent() override final; |
| 273 |
|
| 274 |
bool isDiscardable(); |
| 275 |
bool needsCodegen(); |
| 276 |
|
| 277 |
void accept(Visitor *v) override { v->visit(this); } |
| 278 |
}; |
| 279 |
|
| 280 |
class TemplateMixin final : public TemplateInstance |
| 281 |
{ |
| 282 |
public: |
| 283 |
TypeQualified *tqual; |
| 284 |
|
| 285 |
TemplateMixin *syntaxCopy(Dsymbol *s) override; |
| 286 |
const char *kind() const override; |
| 287 |
bool hasPointers() override; |
| 288 |
|
| 289 |
void accept(Visitor *v) override { v->visit(this); } |
| 290 |
}; |
| 291 |
|
| 292 |
namespace dmd |
| 293 |
{ |
| 294 |
// in templateparamsem.d |
| 295 |
bool tpsemantic(TemplateParameter *tp, Scope *sc, TemplateParameters *parameters); |
| 296 |
|
| 297 |
Expression *isExpression(RootObject *o); |
| 298 |
Dsymbol *isDsymbol(RootObject *o); |
| 299 |
Type *isType(RootObject *o); |
| 300 |
Tuple *isTuple(RootObject *o); |
| 301 |
Parameter *isParameter(RootObject *o); |
| 302 |
TemplateParameter *isTemplateParameter(RootObject *o); |
| 303 |
bool isError(const RootObject *const o); |
| 304 |
void printTemplateStats(bool listInstances, ErrorSink* eSink); |
| 305 |
} |