Skip to content

Commit 30464a5

Browse files
authored
Merge pull request #7597 from slavapestov/introduce-generic-context
AST: Introduce new GenericContext base class
2 parents cd9bc84 + ad78604 commit 30464a5

File tree

2 files changed

+185
-356
lines changed

2 files changed

+185
-356
lines changed

include/swift/AST/Decl.h

Lines changed: 81 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,82 @@ class alignas(RequirementRepr) TrailingWhereClause final :
13741374
}
13751375
};
13761376

1377+
class GenericContext : public DeclContext {
1378+
private:
1379+
GenericParamList *GenericParams = nullptr;
1380+
1381+
/// The trailing where clause.
1382+
///
1383+
/// Note that this is not currently serialized, because semantic analysis
1384+
/// moves the trailing where clause into the generic parameter list.
1385+
TrailingWhereClause *TrailingWhere = nullptr;
1386+
1387+
/// The generic signature or environment of this declaration.
1388+
///
1389+
/// When this declaration stores only a signature, the generic
1390+
/// environment will be lazily loaded.
1391+
mutable llvm::PointerUnion<GenericSignature *, GenericEnvironment *>
1392+
GenericSigOrEnv;
1393+
1394+
/// Lazily populate the generic environment.
1395+
GenericEnvironment *getLazyGenericEnvironmentSlow() const;
1396+
1397+
protected:
1398+
GenericContext(DeclContextKind Kind, DeclContext *Parent)
1399+
: DeclContext(Kind, Parent) { }
1400+
1401+
public:
1402+
/// \brief Retrieve the set of parameters to a generic subscript, or null if
1403+
/// this subscript is not generic.
1404+
GenericParamList *getGenericParams() const { return GenericParams; }
1405+
1406+
void setGenericParams(GenericParamList *GenericParams);
1407+
1408+
/// \brief Determine whether this subscript has generic parameters
1409+
/// of its own.
1410+
bool isGeneric() const { return GenericParams != nullptr; }
1411+
1412+
/// Retrieve the trailing where clause for this extension, if any.
1413+
TrailingWhereClause *getTrailingWhereClause() const {
1414+
return TrailingWhere;
1415+
}
1416+
1417+
/// Set the trailing where clause for this extension.
1418+
void setTrailingWhereClause(TrailingWhereClause *trailingWhereClause) {
1419+
TrailingWhere = trailingWhereClause;
1420+
}
1421+
1422+
/// Retrieve the generic signature for this subscript.
1423+
GenericSignature *getGenericSignature() const;
1424+
1425+
/// Retrieve the generic context for this subscript.
1426+
GenericEnvironment *getGenericEnvironment() const;
1427+
1428+
/// Retrieve the innermost generic parameter types.
1429+
ArrayRef<GenericTypeParamType *> getInnermostGenericParamTypes() const {
1430+
if (auto sig = getGenericSignature())
1431+
return sig->getInnermostGenericParams();
1432+
else
1433+
return { };
1434+
}
1435+
1436+
/// Retrieve the generic requirements.
1437+
ArrayRef<Requirement> getGenericRequirements() const {
1438+
if (auto sig = getGenericSignature())
1439+
return sig->getRequirements();
1440+
else
1441+
return { };
1442+
}
1443+
1444+
/// Set a lazy generic environment.
1445+
void setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
1446+
GenericSignature *genericSig,
1447+
uint64_t genericEnvData);
1448+
1449+
/// Set the generic context of this subscript.
1450+
void setGenericEnvironment(GenericEnvironment *genericEnv);
1451+
};
1452+
13771453
/// Describes what kind of name is being imported.
13781454
///
13791455
/// If the enumerators here are changed, make sure to update all diagnostics
@@ -1483,32 +1559,16 @@ class ImportDecl final : public Decl,
14831559
/// ExtensionDecl - This represents a type extension containing methods
14841560
/// associated with the type. This is not a ValueDecl and has no Type because
14851561
/// there are no runtime values of the Extension's type.
1486-
class ExtensionDecl final : public Decl, public DeclContext,
1562+
class ExtensionDecl final : public Decl, public GenericContext,
14871563
public IterableDeclContext {
14881564
SourceLoc ExtensionLoc; // Location of 'extension' keyword.
14891565
SourceRange Braces;
14901566

14911567
/// The type being extended.
14921568
TypeLoc ExtendedType;
14931569

1494-
/// The generic parameters of the extension.
1495-
GenericParamList *GenericParams = nullptr;
1496-
1497-
/// The generic signature or environment of this extension.
1498-
///
1499-
/// When this extension stores only a signature, the generic environment
1500-
/// will be lazily loaded.
1501-
mutable llvm::PointerUnion<GenericSignature *, GenericEnvironment *>
1502-
GenericSigOrEnv;
1503-
15041570
MutableArrayRef<TypeLoc> Inherited;
15051571

1506-
/// The trailing where clause.
1507-
///
1508-
/// Note that this is not currently serialized, because semantic analysis
1509-
/// moves the trailing where clause into the generic parameter list.
1510-
TrailingWhereClause *TrailingWhere;
1511-
15121572
/// \brief The next extension in the linked list of extensions.
15131573
///
15141574
/// The bit indicates whether this extension has been resolved to refer to
@@ -1543,9 +1603,6 @@ class ExtensionDecl final : public Decl, public DeclContext,
15431603
/// Slow path for \c takeConformanceLoader().
15441604
std::pair<LazyMemberLoader *, uint64_t> takeConformanceLoaderSlow();
15451605

1546-
/// Lazily populate the generic environment.
1547-
GenericEnvironment *getLazyGenericEnvironmentSlow() const;
1548-
15491606
public:
15501607
using Decl::getASTContext;
15511608

@@ -1566,45 +1623,6 @@ class ExtensionDecl final : public Decl, public DeclContext,
15661623
SourceRange getBraces() const { return Braces; }
15671624
void setBraces(SourceRange braces) { Braces = braces; }
15681625

1569-
/// Retrieve the innermost generic parameter list.
1570-
GenericParamList *getGenericParams() const {
1571-
return GenericParams;
1572-
}
1573-
1574-
void setGenericParams(GenericParamList *params);
1575-
1576-
/// Retrieve the trailing where clause for this extension, if any.
1577-
TrailingWhereClause *getTrailingWhereClause() const {
1578-
return TrailingWhere;
1579-
}
1580-
1581-
/// Set the trailing where clause for this extension.
1582-
void setTrailingWhereClause(TrailingWhereClause *trailingWhereClause) {
1583-
TrailingWhere = trailingWhereClause;
1584-
}
1585-
1586-
/// Retrieve the generic requirements.
1587-
ArrayRef<Requirement> getGenericRequirements() const {
1588-
if (auto sig = getGenericSignature())
1589-
return sig->getRequirements();
1590-
else
1591-
return { };
1592-
}
1593-
1594-
/// Retrieve the generic signature for this type.
1595-
GenericSignature *getGenericSignature() const;
1596-
1597-
/// Retrieve the generic context for this type.
1598-
GenericEnvironment *getGenericEnvironment() const;
1599-
1600-
/// Set a lazy generic environment.
1601-
void setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
1602-
GenericSignature *genericSig,
1603-
uint64_t genericEnvData);
1604-
1605-
/// Set the generic context of this extension.
1606-
void setGenericEnvironment(GenericEnvironment *genericEnv);
1607-
16081626
/// Retrieve the type being extended.
16091627
Type getExtendedType() const { return ExtendedType.getType(); }
16101628

@@ -2351,61 +2369,13 @@ class TypeDecl : public ValueDecl {
23512369

23522370
/// A type declaration that can have generic parameters attached to it. Because
23532371
/// it has these generic parameters, it is always a DeclContext.
2354-
class GenericTypeDecl : public TypeDecl, public DeclContext {
2355-
GenericParamList *GenericParams = nullptr;
2356-
2357-
/// The generic signature or environment of this type.
2358-
///
2359-
/// When this function stores only a signature, the generic environment
2360-
/// will be lazily loaded.
2361-
mutable llvm::PointerUnion<GenericSignature *, GenericEnvironment *>
2362-
GenericSigOrEnv;
2363-
2364-
/// Lazily populate the generic environment.
2365-
GenericEnvironment *getLazyGenericEnvironmentSlow() const;
2366-
2372+
class GenericTypeDecl : public TypeDecl, public GenericContext {
23672373
public:
23682374
GenericTypeDecl(DeclKind K, DeclContext *DC,
23692375
Identifier name, SourceLoc nameLoc,
23702376
MutableArrayRef<TypeLoc> inherited,
23712377
GenericParamList *GenericParams);
23722378

2373-
GenericParamList *getGenericParams() const { return GenericParams; }
2374-
2375-
/// Provide the set of parameters to a generic type, or null if
2376-
/// this function is not generic.
2377-
void setGenericParams(GenericParamList *params);
2378-
2379-
/// Retrieve the innermost generic parameter types.
2380-
ArrayRef<GenericTypeParamType *> getInnermostGenericParamTypes() const {
2381-
if (auto sig = getGenericSignature())
2382-
return sig->getInnermostGenericParams();
2383-
else
2384-
return { };
2385-
}
2386-
2387-
/// Retrieve the generic requirements.
2388-
ArrayRef<Requirement> getGenericRequirements() const {
2389-
if (auto sig = getGenericSignature())
2390-
return sig->getRequirements();
2391-
else
2392-
return { };
2393-
}
2394-
2395-
/// Retrieve the generic signature for this type.
2396-
GenericSignature *getGenericSignature() const;
2397-
2398-
/// Retrieve the generic context for this type.
2399-
GenericEnvironment *getGenericEnvironment() const;
2400-
2401-
/// Set a lazy generic environment.
2402-
void setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
2403-
GenericSignature *genericSig,
2404-
uint64_t genericEnvData);
2405-
2406-
/// Set the generic context of this function.
2407-
void setGenericEnvironment(GenericEnvironment *genericEnv);
2408-
24092379
// Resolve ambiguity due to multiple base classes.
24102380
using TypeDecl::getASTContext;
24112381
using DeclContext::operator new;
@@ -4639,7 +4609,7 @@ struct ImportAsMemberStatus {
46394609
};
46404610

46414611
/// \brief Base class for function-like declarations.
4642-
class AbstractFunctionDecl : public ValueDecl, public DeclContext {
4612+
class AbstractFunctionDecl : public ValueDecl, public GenericContext {
46434613
public:
46444614
enum class BodyKind {
46454615
/// The function did not have a body in the source code file.
@@ -4690,15 +4660,6 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
46904660
SourceRange BodyRange;
46914661
};
46924662

4693-
GenericParamList *GenericParams;
4694-
4695-
/// The generic signature or environment of this function.
4696-
///
4697-
/// When this function stores only a signature, the generic environment
4698-
/// will be lazily loaded.
4699-
mutable llvm::PointerUnion<GenericSignature *, GenericEnvironment *>
4700-
GenericSigOrEnv;
4701-
47024663
CaptureInfo Captures;
47034664

47044665
/// Location of the 'throws' token.
@@ -4711,8 +4672,8 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
47114672
unsigned NumParameterLists,
47124673
GenericParamList *GenericParams)
47134674
: ValueDecl(Kind, Parent, Name, NameLoc),
4714-
DeclContext(DeclContextKind::AbstractFunctionDecl, Parent),
4715-
Body(nullptr), GenericParams(nullptr), ThrowsLoc(ThrowsLoc) {
4675+
GenericContext(DeclContextKind::AbstractFunctionDecl, Parent),
4676+
Body(nullptr), ThrowsLoc(ThrowsLoc) {
47164677
setBodyKind(BodyKind::None);
47174678
setGenericParams(GenericParams);
47184679
AbstractFunctionDeclBits.NumParameterLists = NumParameterLists;
@@ -4727,30 +4688,11 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
47274688
AbstractFunctionDeclBits.BodyKind = unsigned(K);
47284689
}
47294690

4730-
void setGenericParams(GenericParamList *GenericParams);
4731-
4732-
/// Lazily populate the generic environment.
4733-
GenericEnvironment *getLazyGenericEnvironmentSlow() const;
4734-
47354691
public:
47364692
/// \brief Should this declaration be treated as if annotated with transparent
47374693
/// attribute.
47384694
bool isTransparent() const;
47394695

4740-
/// Retrieve the generic signature for this function.
4741-
GenericSignature *getGenericSignature() const;
4742-
4743-
/// Retrieve the generic context for this function.
4744-
GenericEnvironment *getGenericEnvironment() const;
4745-
4746-
/// Set a lazy generic environment.
4747-
void setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
4748-
GenericSignature *genericSig,
4749-
uint64_t genericEnvData);
4750-
4751-
/// Set the generic context of this function.
4752-
void setGenericEnvironment(GenericEnvironment *genericEnv);
4753-
47544696
// Expose our import as member status
47554697
bool isImportAsMember() const { return IAMStatus.isImportAsMember(); }
47564698
bool isImportAsInstanceMember() const { return IAMStatus.isInstance(); }
@@ -4930,14 +4872,6 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
49304872
}
49314873
ParamDecl *getImplicitSelfDecl();
49324874

4933-
/// \brief Retrieve the set of parameters to a generic function, or null if
4934-
/// this function is not generic.
4935-
GenericParamList *getGenericParams() const { return GenericParams; }
4936-
4937-
/// \brief Determine whether this is a generic function, which can only be
4938-
/// used when each of the archetypes is bound to a particular concrete type.
4939-
bool isGeneric() const { return GenericParams != nullptr; }
4940-
49414875
/// Retrieve the declaration that this method overrides, if any.
49424876
AbstractFunctionDecl *getOverriddenDecl() const;
49434877

0 commit comments

Comments
 (0)