@@ -120,6 +120,15 @@ namespace Slang
120
120
auto typeRepr = TranslateTypeNodeImpl (node);
121
121
return ExtractTypeFromTypeRepr (typeRepr);
122
122
}
123
+ TypeExp TranslateTypeNodeForced (TypeExp const & typeExp)
124
+ {
125
+ auto typeRepr = TranslateTypeNodeImpl (typeExp.exp );
126
+
127
+ TypeExp result;
128
+ result.exp = typeRepr;
129
+ result.type = ExtractTypeFromTypeRepr (typeRepr);
130
+ return result;
131
+ }
123
132
TypeExp TranslateTypeNode (TypeExp const & typeExp)
124
133
{
125
134
// HACK(tfoley): It seems that in some cases we end up re-checking
@@ -130,14 +139,7 @@ namespace Slang
130
139
{
131
140
return typeExp;
132
141
}
133
-
134
-
135
- auto typeRepr = TranslateTypeNodeImpl (typeExp.exp );
136
-
137
- TypeExp result;
138
- result.exp = typeRepr;
139
- result.type = ExtractTypeFromTypeRepr (typeRepr);
140
- return result;
142
+ return TranslateTypeNodeForced (typeExp);
141
143
}
142
144
143
145
RefPtr<DeclRefType> getExprDeclRefType (Expr * expr)
@@ -153,8 +155,6 @@ namespace Slang
153
155
RefPtr<Expr> baseExpr,
154
156
SourceLoc loc)
155
157
{
156
- if (declRef.As <AssocTypeDecl>())
157
- getNewThisTypeSubst (declRef);
158
158
if (baseExpr)
159
159
{
160
160
RefPtr<Expr> expr;
@@ -1287,16 +1287,34 @@ namespace Slang
1287
1287
varDecl->initExpr = initExpr;
1288
1288
}
1289
1289
1290
+ // Fill in default substitutions for the 'subtype' part of a type constraint decl
1291
+ void CheckConstraintSubType (TypeExp & typeExp)
1292
+ {
1293
+ if (auto sharedTypeExpr = typeExp.exp .As <SharedTypeExpr>())
1294
+ {
1295
+ if (auto declRefType = sharedTypeExpr->base ->AsDeclRefType ())
1296
+ {
1297
+ declRefType->declRef .substitutions = createDefaultSubstitutions (getSession (), declRefType->declRef .getDecl ());
1298
+ if (auto typetype = typeExp.exp ->type .type .As <TypeType>())
1299
+ typetype->type = declRefType;
1300
+ }
1301
+ }
1302
+ }
1303
+
1290
1304
void CheckGenericConstraintDecl (GenericTypeConstraintDecl* decl)
1291
1305
{
1292
1306
// TODO: are there any other validations we can do at this point?
1293
1307
//
1294
1308
// There probably needs to be a kind of "occurs check" to make
1295
1309
// sure that the constraint actually applies to at least one
1296
1310
// of the parameters of the generic.
1297
-
1298
- decl->sub = TranslateTypeNode (decl->sub );
1299
- decl->sup = TranslateTypeNode (decl->sup );
1311
+ if (decl->checkState == DeclCheckState::Unchecked)
1312
+ {
1313
+ decl->checkState = DeclCheckState::Checked;
1314
+ CheckConstraintSubType (decl->sub );
1315
+ decl->sub = TranslateTypeNodeForced (decl->sub );
1316
+ decl->sup = TranslateTypeNodeForced (decl->sup );
1317
+ }
1300
1318
}
1301
1319
1302
1320
void checkDecl (Decl* decl)
@@ -1343,6 +1361,7 @@ namespace Slang
1343
1361
{
1344
1362
// check the type being inherited from
1345
1363
auto base = inheritanceDecl->base ;
1364
+ CheckConstraintSubType (base);
1346
1365
base = TranslateTypeNode (base);
1347
1366
inheritanceDecl->base = base;
1348
1367
@@ -1677,18 +1696,18 @@ namespace Slang
1677
1696
// An associated type requirement should be allowed
1678
1697
// to be satisfied by any type declaration:
1679
1698
// a typedef, a `struct`, etc.
1680
- auto checkSubTypeMember = [&](DeclRef<AggTypeDecl > subStructTypeDeclRef) -> bool
1699
+ auto checkSubTypeMember = [&](DeclRef<ContainerDecl > subStructTypeDeclRef) -> bool
1681
1700
{
1682
1701
EnsureDecl (subStructTypeDeclRef.getDecl ());
1683
1702
// this is a sub type (e.g. nested struct declaration) in an aggregate type
1684
1703
// check if this sub type declaration satisfies the constraints defined by the associated type
1685
1704
if (auto requiredTypeDeclRef = requiredMemberDeclRef.As <AssocTypeDecl>())
1686
1705
{
1687
1706
bool conformance = true ;
1688
- auto inheritanceReqDeclRefs = getMembersOfType<InheritanceDecl >(requiredTypeDeclRef);
1707
+ auto inheritanceReqDeclRefs = getMembersOfType<TypeConstraintDecl >(requiredTypeDeclRef);
1689
1708
for (auto inheritanceReqDeclRef : inheritanceReqDeclRefs)
1690
1709
{
1691
- auto interfaceDeclRefType = inheritanceReqDeclRef.getDecl ()->base .type .As <DeclRefType>();
1710
+ auto interfaceDeclRefType = inheritanceReqDeclRef.getDecl ()->getSup () .type .As <DeclRefType>();
1692
1711
SLANG_ASSERT (interfaceDeclRefType);
1693
1712
auto interfaceDeclRef = interfaceDeclRefType->declRef .As <InterfaceDecl>();
1694
1713
SLANG_ASSERT (interfaceDeclRef);
@@ -1744,20 +1763,22 @@ namespace Slang
1744
1763
// check if the specified type satisfies the constraints defined by the associated type
1745
1764
if (auto requiredTypeDeclRef = requiredMemberDeclRef.As <AssocTypeDecl>())
1746
1765
{
1747
- auto constraintList = getMembersOfType<InheritanceDecl>(requiredTypeDeclRef);
1748
- if (constraintList.Count ())
1766
+ auto declRefType = GetType (typedefDeclRef)->GetCanonicalType ()->As <DeclRefType>();
1767
+ if (!declRefType)
1768
+ return false ;
1769
+
1770
+ if (auto genTypeParamDeclRef = declRefType->declRef .As <GenericTypeParamDecl>())
1749
1771
{
1750
- auto declRefType = GetType (typedefDeclRef)->GetCanonicalType ()->As <DeclRefType>();
1751
- if (!declRefType)
1752
- return false ;
1772
+ // TODO: check generic type parameter satisfies constraints
1773
+ return true ;
1774
+ }
1775
+
1753
1776
1754
- auto structTypeDeclRef = declRefType->declRef .As <AggTypeDecl >();
1755
- if (!structTypeDeclRef )
1756
- return false ;
1777
+ auto containerDeclRef = declRefType->declRef .As <ContainerDecl >();
1778
+ if (!containerDeclRef )
1779
+ return false ;
1757
1780
1758
- return checkSubTypeMember (structTypeDeclRef);
1759
- }
1760
- return true ;
1781
+ return checkSubTypeMember (containerDeclRef);
1761
1782
}
1762
1783
}
1763
1784
// Default: just assume that thing aren't being satisfied.
@@ -2496,6 +2517,7 @@ namespace Slang
2496
2517
// TODO: This needs to bottleneck through the common variable checks
2497
2518
2498
2519
para->type = CheckUsableType (para->type );
2520
+
2499
2521
if (para->type .Equals (getSession ()->getVoidType ()))
2500
2522
{
2501
2523
if (!isRewriteMode ())
@@ -6154,7 +6176,6 @@ namespace Slang
6154
6176
return expr;
6155
6177
6156
6178
expr->type = QualType (getSession ()->getErrorType ());
6157
-
6158
6179
auto lookupResult = lookUp (
6159
6180
getSession (),
6160
6181
this , expr->name , expr->scope );
@@ -6970,29 +6991,34 @@ namespace Slang
6970
6991
Decl* decl,
6971
6992
SubstitutionSet parentSubst)
6972
6993
{
6994
+ SubstitutionSet resultSubst = parentSubst;
6995
+ if (auto interfaceDecl = dynamic_cast <InterfaceDecl*>(decl))
6996
+ {
6997
+ resultSubst.thisTypeSubstitution = new ThisTypeSubstitution ();
6998
+ }
6973
6999
auto dd = decl->ParentDecl ;
6974
7000
if ( auto genericDecl = dynamic_cast <GenericDecl*>(dd) )
6975
7001
{
6976
7002
// We don't want to specialize references to anything
6977
7003
// other than the "inner" declaration itself.
6978
7004
if (decl != genericDecl->inner )
6979
- return parentSubst ;
7005
+ return resultSubst ;
6980
7006
6981
- SubstitutionSet resultSubst = parentSubst;
6982
7007
RefPtr<GenericSubstitution> subst = new GenericSubstitution ();
6983
7008
subst->genericDecl = genericDecl;
6984
7009
subst->outer = parentSubst.genericSubstitutions ;
6985
7010
resultSubst.genericSubstitutions = subst;
6986
-
7011
+ SubstitutionSet outerSubst = resultSubst;
7012
+ outerSubst.genericSubstitutions = outerSubst.genericSubstitutions ?outerSubst.genericSubstitutions ->outer :nullptr ;
6987
7013
for ( auto mm : genericDecl->Members )
6988
7014
{
6989
7015
if ( auto genericTypeParamDecl = mm.As <GenericTypeParamDecl>() )
6990
7016
{
6991
- subst->args .Add (DeclRefType::Create (session, makeDeclRef (genericTypeParamDecl.Ptr ())));
7017
+ subst->args .Add (DeclRefType::Create (session, DeclRef<Decl> (genericTypeParamDecl.Ptr (), outerSubst )));
6992
7018
}
6993
7019
else if ( auto genericValueParamDecl = mm.As <GenericValueParamDecl>() )
6994
7020
{
6995
- subst->args .Add (new GenericParamIntVal (makeDeclRef (genericValueParamDecl.Ptr ())));
7021
+ subst->args .Add (new GenericParamIntVal (DeclRef<GenericValueParamDecl> (genericValueParamDecl.Ptr (), outerSubst )));
6996
7022
}
6997
7023
}
6998
7024
@@ -7002,15 +7028,14 @@ namespace Slang
7002
7028
if (auto genericTypeConstraintDecl = mm.As <GenericTypeConstraintDecl>())
7003
7029
{
7004
7030
RefPtr<DeclaredSubtypeWitness> witness = new DeclaredSubtypeWitness ();
7005
- witness->declRef = makeDeclRef (genericTypeConstraintDecl.Ptr ());
7031
+ witness->declRef = DeclRef<Decl> (genericTypeConstraintDecl.Ptr (), outerSubst );
7006
7032
witness->sub = genericTypeConstraintDecl->sub .type ;
7007
7033
witness->sup = genericTypeConstraintDecl->sup .type ;
7008
7034
subst->args .Add (witness);
7009
7035
}
7010
7036
}
7011
- return resultSubst;
7012
7037
}
7013
- return parentSubst ;
7038
+ return resultSubst ;
7014
7039
}
7015
7040
7016
7041
SubstitutionSet createDefaultSubstitutions (
0 commit comments