@@ -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)
@@ -1287,16 +1289,34 @@ namespace Slang
1287
1289
varDecl->initExpr = initExpr;
1288
1290
}
1289
1291
1292
+ // Fill in default substitutions for the 'subtype' part of a type constraint decl
1293
+ void CheckConstraintSubType (TypeExp & typeExp)
1294
+ {
1295
+ if (auto sharedTypeExpr = typeExp.exp .As <SharedTypeExpr>())
1296
+ {
1297
+ if (auto declRefType = sharedTypeExpr->base ->AsDeclRefType ())
1298
+ {
1299
+ declRefType->declRef .substitutions = createDefaultSubstitutions (getSession (), declRefType->declRef .getDecl ());
1300
+ if (auto typetype = typeExp.exp ->type .type .As <TypeType>())
1301
+ typetype->type = declRefType;
1302
+ }
1303
+ }
1304
+ }
1305
+
1290
1306
void CheckGenericConstraintDecl (GenericTypeConstraintDecl* decl)
1291
1307
{
1292
1308
// TODO: are there any other validations we can do at this point?
1293
1309
//
1294
1310
// There probably needs to be a kind of "occurs check" to make
1295
1311
// sure that the constraint actually applies to at least one
1296
1312
// of the parameters of the generic.
1297
-
1298
- decl->sub = TranslateTypeNode (decl->sub );
1299
- decl->sup = TranslateTypeNode (decl->sup );
1313
+ if (decl->checkState == DeclCheckState::Unchecked)
1314
+ {
1315
+ decl->checkState = DeclCheckState::Checked;
1316
+ CheckConstraintSubType (decl->sub );
1317
+ decl->sub = TranslateTypeNodeForced (decl->sub );
1318
+ decl->sup = TranslateTypeNodeForced (decl->sup );
1319
+ }
1300
1320
}
1301
1321
1302
1322
void checkDecl (Decl* decl)
@@ -1343,6 +1363,7 @@ namespace Slang
1343
1363
{
1344
1364
// check the type being inherited from
1345
1365
auto base = inheritanceDecl->base ;
1366
+ CheckConstraintSubType (base);
1346
1367
base = TranslateTypeNode (base);
1347
1368
inheritanceDecl->base = base;
1348
1369
@@ -1677,18 +1698,18 @@ namespace Slang
1677
1698
// An associated type requirement should be allowed
1678
1699
// to be satisfied by any type declaration:
1679
1700
// a typedef, a `struct`, etc.
1680
- auto checkSubTypeMember = [&](DeclRef<AggTypeDecl > subStructTypeDeclRef) -> bool
1701
+ auto checkSubTypeMember = [&](DeclRef<ContainerDecl > subStructTypeDeclRef) -> bool
1681
1702
{
1682
1703
EnsureDecl (subStructTypeDeclRef.getDecl ());
1683
1704
// this is a sub type (e.g. nested struct declaration) in an aggregate type
1684
1705
// check if this sub type declaration satisfies the constraints defined by the associated type
1685
1706
if (auto requiredTypeDeclRef = requiredMemberDeclRef.As <AssocTypeDecl>())
1686
1707
{
1687
1708
bool conformance = true ;
1688
- auto inheritanceReqDeclRefs = getMembersOfType<InheritanceDecl >(requiredTypeDeclRef);
1709
+ auto inheritanceReqDeclRefs = getMembersOfType<TypeConstraintDecl >(requiredTypeDeclRef);
1689
1710
for (auto inheritanceReqDeclRef : inheritanceReqDeclRefs)
1690
1711
{
1691
- auto interfaceDeclRefType = inheritanceReqDeclRef.getDecl ()->base .type .As <DeclRefType>();
1712
+ auto interfaceDeclRefType = inheritanceReqDeclRef.getDecl ()->getSup () .type .As <DeclRefType>();
1692
1713
SLANG_ASSERT (interfaceDeclRefType);
1693
1714
auto interfaceDeclRef = interfaceDeclRefType->declRef .As <InterfaceDecl>();
1694
1715
SLANG_ASSERT (interfaceDeclRef);
@@ -1744,20 +1765,22 @@ namespace Slang
1744
1765
// check if the specified type satisfies the constraints defined by the associated type
1745
1766
if (auto requiredTypeDeclRef = requiredMemberDeclRef.As <AssocTypeDecl>())
1746
1767
{
1747
- auto constraintList = getMembersOfType<InheritanceDecl>(requiredTypeDeclRef);
1748
- if (constraintList.Count ())
1768
+ auto declRefType = GetType (typedefDeclRef)->GetCanonicalType ()->As <DeclRefType>();
1769
+ if (!declRefType)
1770
+ return false ;
1771
+
1772
+ if (auto genTypeParamDeclRef = declRefType->declRef .As <GenericTypeParamDecl>())
1749
1773
{
1750
- auto declRefType = GetType (typedefDeclRef)->GetCanonicalType ()->As <DeclRefType>();
1751
- if (!declRefType)
1752
- return false ;
1774
+ // TODO: check generic type parameter satisfies constraints
1775
+ return true ;
1776
+ }
1777
+
1753
1778
1754
- auto structTypeDeclRef = declRefType->declRef .As <AggTypeDecl >();
1755
- if (!structTypeDeclRef )
1756
- return false ;
1779
+ auto containerDeclRef = declRefType->declRef .As <ContainerDecl >();
1780
+ if (!containerDeclRef )
1781
+ return false ;
1757
1782
1758
- return checkSubTypeMember (structTypeDeclRef);
1759
- }
1760
- return true ;
1783
+ return checkSubTypeMember (containerDeclRef);
1761
1784
}
1762
1785
}
1763
1786
// Default: just assume that thing aren't being satisfied.
@@ -2496,6 +2519,7 @@ namespace Slang
2496
2519
// TODO: This needs to bottleneck through the common variable checks
2497
2520
2498
2521
para->type = CheckUsableType (para->type );
2522
+
2499
2523
if (para->type .Equals (getSession ()->getVoidType ()))
2500
2524
{
2501
2525
if (!isRewriteMode ())
@@ -6154,7 +6178,6 @@ namespace Slang
6154
6178
return expr;
6155
6179
6156
6180
expr->type = QualType (getSession ()->getErrorType ());
6157
-
6158
6181
auto lookupResult = lookUp (
6159
6182
getSession (),
6160
6183
this , expr->name , expr->scope );
@@ -6983,16 +7006,17 @@ namespace Slang
6983
7006
subst->genericDecl = genericDecl;
6984
7007
subst->outer = parentSubst.genericSubstitutions ;
6985
7008
resultSubst.genericSubstitutions = subst;
6986
-
7009
+ SubstitutionSet outerSubst = resultSubst;
7010
+ outerSubst.genericSubstitutions = outerSubst.genericSubstitutions ?outerSubst.genericSubstitutions ->outer :nullptr ;
6987
7011
for ( auto mm : genericDecl->Members )
6988
7012
{
6989
7013
if ( auto genericTypeParamDecl = mm.As <GenericTypeParamDecl>() )
6990
7014
{
6991
- subst->args .Add (DeclRefType::Create (session, makeDeclRef (genericTypeParamDecl.Ptr ())));
7015
+ subst->args .Add (DeclRefType::Create (session, DeclRef<Decl> (genericTypeParamDecl.Ptr (), outerSubst )));
6992
7016
}
6993
7017
else if ( auto genericValueParamDecl = mm.As <GenericValueParamDecl>() )
6994
7018
{
6995
- subst->args .Add (new GenericParamIntVal (makeDeclRef (genericValueParamDecl.Ptr ())));
7019
+ subst->args .Add (new GenericParamIntVal (DeclRef<GenericValueParamDecl> (genericValueParamDecl.Ptr (), outerSubst )));
6996
7020
}
6997
7021
}
6998
7022
@@ -7002,7 +7026,7 @@ namespace Slang
7002
7026
if (auto genericTypeConstraintDecl = mm.As <GenericTypeConstraintDecl>())
7003
7027
{
7004
7028
RefPtr<DeclaredSubtypeWitness> witness = new DeclaredSubtypeWitness ();
7005
- witness->declRef = makeDeclRef (genericTypeConstraintDecl.Ptr ());
7029
+ witness->declRef = DeclRef<Decl> (genericTypeConstraintDecl.Ptr (), outerSubst );
7006
7030
witness->sub = genericTypeConstraintDecl->sub .type ;
7007
7031
witness->sup = genericTypeConstraintDecl->sup .type ;
7008
7032
subst->args .Add (witness);
0 commit comments