Skip to content

Commit 12e228c

Browse files
committed
Introduce a new representation of polymorphic function types.
Introduces a new kind of function type, GenericFunctionType, that represents a polymorphic function type with all of its generic parameters and requirements stored in a more readily canonicalizable form. It is meant to eventually replace PolymorphicFunctionType, but for now we build it up in parallel so we can switch over to it pieacemeal. Note: this representation is built and then thrown away. We'll start recording it soon. Swift SVN r8881
1 parent 781dcf7 commit 12e228c

23 files changed

+994
-160
lines changed

include/swift/AST/ArchetypeBuilder.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ArchetypeType;
3232
class AssociatedTypeDecl;
3333
class Pattern;
3434
class ProtocolDecl;
35-
class Requirement;
35+
class RequirementRepr;
3636
class SourceLoc;
3737
class TranslationUnit;
3838
class Type;
@@ -104,6 +104,9 @@ class ArchetypeBuilder {
104104
ArchetypeBuilder(ArchetypeBuilder &&);
105105
~ArchetypeBuilder();
106106

107+
/// Retrieve the translation unit.
108+
TranslationUnit &getTranslationUnit() const { return TU; }
109+
107110
/// \brief Add a new generic parameter for which there may be requirements.
108111
///
109112
/// \returns true if an error occurred, false otherwise.
@@ -114,7 +117,7 @@ class ArchetypeBuilder {
114117
///
115118
/// \returns true if this requirement makes the set of requirements
116119
/// inconsistent, in which case a diagnostic will have been issued.
117-
bool addRequirement(const Requirement &Req);
120+
bool addRequirement(const RequirementRepr &Req);
118121

119122
/// \brief Add a new, implicit conformance requirement for one of the
120123
/// parameters.
@@ -180,7 +183,6 @@ class ArchetypeBuilder {
180183
/// list.
181184
ArrayRef<ArchetypeType *> getAllArchetypes();
182185

183-
// FIXME: Infer requirements from signatures
184186
// FIXME: Compute the set of 'extra' witness tables needed to express this
185187
// requirement set.
186188

@@ -233,6 +235,10 @@ class ArchetypeBuilder::PotentialArchetype {
233235
/// \brief Retrieve the full display name of this potential archetype.
234236
std::string getFullName() const;
235237

238+
/// Retrieve the parent of this potential archetype, which will be non-null
239+
/// when this potential archetype is an associated type.
240+
PotentialArchetype *getParent() const { return Parent; }
241+
236242
/// Retrieve the set of protocols to which this type conforms.
237243
ArrayRef<ProtocolDecl *> getConformsTo() const {
238244
return llvm::makeArrayRef(ConformsTo.begin(), ConformsTo.end());
@@ -241,6 +247,11 @@ class ArchetypeBuilder::PotentialArchetype {
241247
/// Retrieve the superclass of this archetype.
242248
Type getSuperclass() const { return Superclass; }
243249

250+
/// Retrieve the set of nested types.
251+
const llvm::DenseMap<Identifier, PotentialArchetype*> &getNestedTypes() const{
252+
return NestedTypes;
253+
}
254+
244255
/// \brief Determine the nesting depth of this potential archetype, e.g.,
245256
/// the number of associated type references.
246257
unsigned getNestingDepth() const;
@@ -257,6 +268,9 @@ class ArchetypeBuilder::PotentialArchetype {
257268
ArchetypeType *getArchetype(AssociatedTypeDecl * /*nullable*/ rootAssocTy,
258269
TranslationUnit &tu);
259270

271+
/// Retrieve the associated type declaration for a given nested type.
272+
AssociatedTypeDecl *getAssociatedType(TranslationUnit &tu, Identifier name);
273+
260274
void dump(llvm::raw_ostream &Out, unsigned Indent);
261275

262276
friend class ArchetypeBuilder;

include/swift/AST/Decl.h

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/DefaultArgumentKind.h"
2424
#include "swift/AST/KnownProtocols.h"
2525
#include "swift/AST/Identifier.h"
26+
#include "swift/AST/Requirement.h"
2627
#include "swift/AST/Substitution.h"
2728
#include "swift/AST/Type.h"
2829
#include "swift/AST/TypeLoc.h"
@@ -427,30 +428,19 @@ class GenericParam {
427428
void setDeclContext(DeclContext *DC);
428429
};
429430

430-
/// \brief Describes the kind of a requirement that occurs within a requirements
431-
/// clause.
432-
enum class RequirementKind : unsigned int {
433-
/// \brief A conformance requirement T : P, where T is a type that depends
434-
/// on a generic parameter and P is a protocol to which T must conform.
435-
Conformance,
436-
/// \brief A same-type requirement T == U, where T and U are types that
437-
/// shall be equivalent.
438-
SameType
439-
};
440-
441431
/// \brief A single requirement in a 'where' clause, which places additional
442432
/// restrictions on the generic parameters or associated types of a generic
443433
/// function, type, or protocol.
444434
///
445435
/// This always represents a requirement spelled in the source code. It is
446436
/// never generated implicitly.
447-
class Requirement {
437+
class RequirementRepr {
448438
SourceLoc SeparatorLoc;
449439
RequirementKind Kind : 1;
450440
bool Invalid : 1;
451441
TypeLoc Types[2];
452442

453-
Requirement(SourceLoc SeparatorLoc, RequirementKind Kind, TypeLoc FirstType,
443+
RequirementRepr(SourceLoc SeparatorLoc, RequirementKind Kind, TypeLoc FirstType,
454444
TypeLoc SecondType)
455445
: SeparatorLoc(SeparatorLoc), Kind(Kind), Invalid(false),
456446
Types{FirstType, SecondType} { }
@@ -464,7 +454,7 @@ class Requirement {
464454
/// this requirement was implied.
465455
/// \param Constraint The protocol or protocol composition to which the
466456
/// subject must conform, or superclass from which the subject must inherit.
467-
static Requirement getConformance(TypeLoc Subject,
457+
static RequirementRepr getConformance(TypeLoc Subject,
468458
SourceLoc ColonLoc,
469459
TypeLoc Constraint) {
470460
return { ColonLoc, RequirementKind::Conformance, Subject, Constraint };
@@ -476,7 +466,7 @@ class Requirement {
476466
/// \param EqualLoc The location of the '==' in the same-type constraint, or
477467
/// an invalid location if this requirement was implied.
478468
/// \param SecondType The second type.
479-
static Requirement getSameType(TypeLoc FirstType,
469+
static RequirementRepr getSameType(TypeLoc FirstType,
480470
SourceLoc EqualLoc,
481471
TypeLoc SecondType) {
482472
return { EqualLoc, RequirementKind::SameType, FirstType, SecondType };
@@ -589,15 +579,15 @@ class GenericParamList {
589579
SourceRange Brackets;
590580
unsigned NumParams;
591581
SourceLoc WhereLoc;
592-
MutableArrayRef<Requirement> Requirements;
582+
MutableArrayRef<RequirementRepr> Requirements;
593583
ArrayRef<ArchetypeType *> AllArchetypes;
594584

595585
GenericParamList *OuterParameters;
596586

597587
GenericParamList(SourceLoc LAngleLoc,
598588
ArrayRef<GenericParam> Params,
599589
SourceLoc WhereLoc,
600-
MutableArrayRef<Requirement> Requirements,
590+
MutableArrayRef<RequirementRepr> Requirements,
601591
SourceLoc RAngleLoc);
602592

603593
public:
@@ -630,7 +620,7 @@ class GenericParamList {
630620
SourceLoc LAngleLoc,
631621
ArrayRef<GenericParam> Params,
632622
SourceLoc WhereLoc,
633-
MutableArrayRef<Requirement> Requirements,
623+
MutableArrayRef<RequirementRepr> Requirements,
634624
SourceLoc RAngleLoc);
635625

636626
MutableArrayRef<GenericParam> getParams() {
@@ -659,15 +649,15 @@ class GenericParamList {
659649
/// This list may contain both explicitly-written requirements as well as
660650
/// implicitly-generated requirements, and may be non-empty even if no
661651
/// 'where' keyword is present.
662-
MutableArrayRef<Requirement> getRequirements() { return Requirements; }
652+
MutableArrayRef<RequirementRepr> getRequirements() { return Requirements; }
663653

664654
/// \brief Retrieve the set of additional requirements placed on these
665655
/// generic parameters and types derived from them.
666656
///
667657
/// This list may contain both explicitly-written requirements as well as
668658
/// implicitly-generated requirements, and may be non-empty even if no
669659
/// 'where' keyword is present.
670-
ArrayRef<Requirement> getRequirements() const { return Requirements; }
660+
ArrayRef<RequirementRepr> getRequirements() const { return Requirements; }
671661

672662
/// \brief Override the set of requirements associated with this generic
673663
/// parameter list.
@@ -676,7 +666,7 @@ class GenericParamList {
676666
/// to be a superset of the existing set of requirements (although this
677667
/// property is not checked here). It is assumed that the array reference
678668
/// refers to ASTContext-allocated memory.
679-
void overrideRequirements(MutableArrayRef<Requirement> NewRequirements) {
669+
void overrideRequirements(MutableArrayRef<RequirementRepr> NewRequirements) {
680670
Requirements = NewRequirements;
681671
}
682672

@@ -887,8 +877,8 @@ class ExtensionDecl : public Decl, public DeclContext {
887877
}
888878

889879
/// Note that we have already type-checked the inheritance clause.
890-
void setCheckedInheritanceClause() {
891-
ExtensionDeclBits.CheckedInheritanceClause = true;
880+
void setCheckedInheritanceClause(bool checked = true) {
881+
ExtensionDeclBits.CheckedInheritanceClause = checked;
892882
}
893883

894884
/// \brief Retrieve the set of protocols to which this extension conforms.
@@ -1158,8 +1148,8 @@ class TypeDecl : public ValueDecl {
11581148
}
11591149

11601150
/// Note that we have already type-checked the inheritance clause.
1161-
void setCheckedInheritanceClause() {
1162-
TypeDeclBits.CheckedInheritanceClause = true;
1151+
void setCheckedInheritanceClause(bool checked = true) {
1152+
TypeDeclBits.CheckedInheritanceClause = checked;
11631153
}
11641154

11651155
/// \brief Retrieve the set of protocols to which this type conforms.

include/swift/AST/Requirement.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- Requirement.h - Swift Requirement ASTs -----------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the Requirement class and subclasses.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_AST_REQUIREMENT_H
18+
#define SWIFT_AST_REQUIREMENT_H
19+
20+
#include "swift/AST/Type.h"
21+
#include "llvm/ADT/PointerIntPair.h"
22+
23+
namespace swift {
24+
25+
/// \brief Describes the kind of a requirement that occurs within a requirements
26+
/// clause.
27+
enum class RequirementKind : unsigned int {
28+
/// \brief A conformance requirement T : P, where T is a type that depends
29+
/// on a generic parameter and P is a protocol to which T must conform.
30+
Conformance,
31+
/// \brief A same-type requirement T == U, where T and U are types that
32+
/// shall be equivalent.
33+
SameType
34+
};
35+
36+
/// \brief A single requirement placed on the type parameters (or associated
37+
/// types thereof) of a
38+
class Requirement {
39+
llvm::PointerIntPair<Type, 1, RequirementKind> FirstTypeAndKind;
40+
Type SecondType;
41+
42+
public:
43+
/// Create a conformance or same-type requirement.
44+
Requirement(RequirementKind kind, Type first, Type second)
45+
: FirstTypeAndKind(first, kind), SecondType(second) { }
46+
47+
/// \brief Determine the kind of requirement.
48+
RequirementKind getKind() const { return FirstTypeAndKind.getInt(); }
49+
50+
/// \brief Retrieve the first type.
51+
Type getFirstType() const {
52+
return FirstTypeAndKind.getPointer();
53+
}
54+
55+
/// \brief Retrieve the second type.
56+
Type getSecondType() const {
57+
return SecondType;
58+
}
59+
};
60+
61+
} // end namespace swift
62+
63+
#endif

include/swift/AST/TypeNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ TYPE(DependentMember, Type)
117117
ABSTRACT_TYPE(AnyFunction, Type)
118118
TYPE(Function, AnyFunctionType)
119119
TYPE(PolymorphicFunction, AnyFunctionType)
120-
TYPE_RANGE(AnyFunction, Function, PolymorphicFunction)
120+
TYPE(GenericFunction, AnyFunctionType)
121+
TYPE_RANGE(AnyFunction, Function, GenericFunction)
121122
TYPE(Array, Type)
122123
ABSTRACT_SUGARED_TYPE(SyntaxSugar, Type)
123124
SUGARED_TYPE(ArraySlice, SyntaxSugarType)

0 commit comments

Comments
 (0)