Commit Graph

2734 Commits

Author SHA1 Message Date
John McCall
ae978371ac Make calls to non-static methods on classes use virtual
dispatch.  Currently there is no possibility of override.

This was really not as difficult as I managed to make it
the first time through.

Swift SVN r2960
2012-10-10 01:31:47 +00:00
Eli Friedman
50e21a838c More semantic analysis for classes inheriting from generic classes.
Swift SVN r2736
2012-08-24 00:01:24 +00:00
Doug Gregor
b4c91a34a0 Whether a canonical type has been constructed for a given type is not
a reliable way to track whether a particular type has been
validated. Instead, add some bits to the type to indicate which stage
of checking it has received. I hate it, but it works and I don't know
of a better way to ensure that types get validated. This subsystem
will need to get rearchitected at some point (ugh).

Reduce the number of places where we build new BoundGenericTypes,
since they need to be validated fully to get substitutions, and then
introduces a number of validateTypeSimple() calls to validate types in
places where we know the validation will succeed, but we need that
information regardless.


Swift SVN r2681
2012-08-18 00:09:56 +00:00
Eli Friedman
bef302d2ee Initial attempt at implementing semantic analysis of overriding for classes.
Still a lot of missing pieces, but it mostly works.



Swift SVN r2633
2012-08-15 01:21:35 +00:00
John McCall
88e03293cc Change the formal type of subscript declarations to always
pass the index as a separate argument.  This makes it much
easier to work with these things generically.

Swift SVN r2616
2012-08-13 09:02:37 +00:00
Eli Friedman
6781f56cfd Start of support for class inheritance.
Swift SVN r2598
2012-08-09 21:56:05 +00:00
Doug Gregor
051a7dff91 Make sure that class/struct/oneof types are canonical once they have
been type-checked. The initialize constructed types are not (and
cannot be) canonical because archetypes aren't assigned until the
generic parameters in the current and outer contexts are
type-checked.

Test forthcoming as part of IRgen support for nested generics.


Swift SVN r2569
2012-08-06 20:24:24 +00:00
Doug Gregor
531583e2d1 Implement support for nested generics in the AST and type
checker. There are a few related sets of changes here:

  - Generic parameter lists have a link  to their "outer" generic
  parameter lists, so its easy to establish the full generic context
  of an entity.
  - Bound and unbound generic types now carry a parent type, so that
  we distinguish between, e.g., X<Int>.Inner<Int> and
  X<Double>.Inner<Int>. Deduction, substitution, canonicalization,
  etc. cope with the parent type.
  - Opening of polymorphic types now handles multiple levels of
  generic parameters when needed (e.g., when we're substituting into
  the base).

Note that the generics module implied by this representation restricts
what one can do with requirements clauses in nested generics. For
example, one cannot add requirements to outer generic parameters or
their associated types, e.g., this is ill-formed:

  struct X<T : Range> {
    func f<U requires T.Element : Range>() {}
  }

The restriction has some precedent (e.g., in C#), but could be
loosened by rearchitecting how we handle archetypes in nested
generics. The current approach is more straightforward.


Swift SVN r2568
2012-08-06 19:02:06 +00:00
Eli Friedman
3d0d23b3ce Stop use OneOfElementDecls to represent constructors in structs.
Swift SVN r2518
2012-08-03 00:53:36 +00:00
Doug Gregor
3eed1fbafd Generalize the substitution of a "base type" into a reference to a
member of a oneof/struct/class/extension to support types nested
within generic classes, e.g., Vector<Int>.ElementRange. 

Most importantly, nominal types are no longer inherently canonical. A
nominal type refers to both a particular nominal type declaration as
well as its parent, which may be non-canonical and will vary. For
example, the variance in the parent makes Vector<Int>.ElementRange and
Vector<Float>.ElementRange different types. 

Introduce deduction and substitution for nominal types. Deduction is
particular interesting because we actually do allow deduction of T
when comparing X<T>.Inner and X<Int>.Inner, because (unlike C++) there
is no specialization to thwart us.



Swift SVN r2507
2012-08-02 21:22:44 +00:00
Ted Kremenek
81169fc0aa Reduce 'threading' of show colors logic through AST printer API, computing it in one central place.
Swift SVN r2499
2012-07-31 05:10:49 +00:00
Ted Kremenek
c03dce396b Hack up some initial support for coloring -ast-dump to make this easier to read.
Swift SVN r2498
2012-07-31 01:00:55 +00:00
Doug Gregor
ea4cb398ed Destructors of generic class types have polymorphic function
type. This has remarkably little effect on type checking, because the
destructors themselves are never referenced by the AST after initially
type-checking them.


Swift SVN r2474
2012-07-27 06:38:32 +00:00
Doug Gregor
f4ced2028d Make the constructors of generic types polymorphic function types,
including the weird implicit one-of element declarations that end up in struct
types. Teach overload resolution and the type-checking of
ConstructorRefExprs how to specialize these polymorphic function types.


Swift SVN r2473
2012-07-27 06:33:04 +00:00
Doug Gregor
17422d5bee The type of an instance method in a generic class is actually a
polymorphic function type. For example, given

  struct X<T> {
    func f(a : T) -> Int { }
  }

The type of X.f is

  <T> (this : [byref] X<T>) -> (a : T) -> Int

If we have a call to f, e.g.,

  var xi : X<Int>
  xi.f(5)

it will be represented as X.f specialized to type

  (this : [byref] X<Int>) -> (a : Int) -> Int

and then called with 'xi' (DotSyntaxCallExpr) and finally 5
(ApplyExpr). The actual deduction of arguments is not as clean as I'd
like, generic functions of generic classes are unsupported, static
functions are broken, and constructors/destructors are broken. Fixes
for those cases will follow.



Swift SVN r2470
2012-07-27 00:24:32 +00:00
Doug Gregor
67c2354d28 When building archetypes, gather all of the archetypes (both primary
and derived) so they can be stored within the generic parameter list
for use 'later'.

More immediately, when we deduce arguments for a polymorphic function
type, check that all of the derived archetypes conform to all of the
protocol requirements, stashing that protocol-conformance information
in the coercion context (also for use 'later'). From a type-checking
perspective, we now actually verify requirements on associated types
such as the requirement on R.Element in, e.g.,

  func minElement<R : Range requires R.Element : Ordered>(range : R)
         -> R.Element



Swift SVN r2460
2012-07-26 00:24:05 +00:00
Doug Gregor
72e69d5420 Teach each archetype type to keep track of its nested types, so that
type substitution for a nested type reference (Foo.Bar.Wibble) whose
substituted parent reference (Foo.Bar) yields an archetype can simply
look for the appropriate nested type in the archetype. 

This allows us to eliminate the hideous ASTContext::AssociatedTypeMap
and simply the archetype builder.


Swift SVN r2438
2012-07-24 23:52:04 +00:00
Doug Gregor
aef8110375 Switch archetype assignment for protocols over to the archetype
builder. For this to work, the 'parameter' used to seed the archetype
builder is the 'This' associated type, which implicitly conforms to
the protocol. All other archetype assignments flow from that
archetype.

In the same vein, when faking a polymorphic function type for an
operator found within a protocol, we only need to substitute for the
'This' associated type. The rest are derived due to the implicit
requirement that 'This' conforms to its protocol.

Finally, because archetype assignment within protocols occurs during
type-checking, and that archetype assignment may visit any protocol
visible in the translation unit, we need to validate the types used in
archetype assignment for *all* protocols before attempting archetype
assignment on any one protocol. Thus, perform this type validation
before the main declaration type-checking phase.



Swift SVN r2436
2012-07-24 22:16:41 +00:00
Eli Friedman
b822876a6a Push TypeLocs through requires clauses.
Swift SVN r2432
2012-07-24 20:02:57 +00:00
Doug Gregor
7777cb22fe Clamp down on the types specified in requirements clauses a bit. The
subject of a conformance constraint (e.g., T : Foo) and both types of
a same-type constraint need to be generic parameters of nested types
thereof. The latter actually prohibits code like, e.g.,

  func f<R : Range requires R.Element == Int>()

which is something we may very well want to bring back into the
language in the future.


Swift SVN r2411
2012-07-23 21:13:33 +00:00
Eli Friedman
c6901f6a6c Initial parsing/AST for generic constructors.
Swift SVN r2387
2012-07-20 23:07:40 +00:00
Eli Friedman
d6a4ba90dd Move TypeLocs to a design where a TypeLoc is a struct containing a type plus
location info for that type.  Propagate TypeLocs a bit more through the AST.



Swift SVN r2383
2012-07-20 21:00:30 +00:00
Eli Friedman
e592e07f2e Compute full SourceRanges for Decls.
Swift SVN r2376
2012-07-19 22:27:13 +00:00
Eli Friedman
7330aca582 Start propagating TypeLocs into TypeChecker::validateType.
Swift SVN r2373
2012-07-19 03:40:30 +00:00
Eli Friedman
361f931a16 Start propgating TypeLocs from the parser into the AST.
Swift SVN r2372
2012-07-19 02:54:28 +00:00
Doug Gregor
27e3a03525 Implement parsing, AST, and basic validation of requires clauses
within generic parameter clauses, e.g.,

  func f<T requires T : P1, T : P2>() {}




Swift SVN r2369
2012-07-18 23:36:56 +00:00
Eli Friedman
6b4a248f04 Parsing and AST support for destructors on classes.
Swift SVN r2348
2012-07-12 01:26:02 +00:00
Eli Friedman
0aee5f30d5 A few misc fixes for constructing generic types. Add a WIP implementation of Slice<T>.
Swift SVN r2334
2012-07-10 21:56:43 +00:00
Eli Friedman
bc5bb8e9f9 Fix the computation of the type of the elements of a generic oneof.
Swift SVN r2327
2012-07-10 03:25:08 +00:00
Eli Friedman
ab7d855a40 Fix a couple places where we weren't calling validateType early enough.
Swift SVN r2311
2012-07-06 00:55:28 +00:00
Eli Friedman
a54c5493da Per discussion with Doug, add UnboundGenericType and BoundGenericType to represent generic types.
Swift SVN r2304
2012-07-04 01:17:16 +00:00
Eli Friedman
99fac3aaa8 Change MetaTypeType so that the instance type is actually a Type, not a TypeDecl*.
Swift SVN r2301
2012-07-03 23:12:19 +00:00
Eli Friedman
2268f7155d Introduce UnresolvedNominalType to represent a generic type without an argument list, and add an argument list to NominalType to represent a generic type including an argument list.
Swift SVN r2299
2012-07-03 22:15:02 +00:00
Doug Gregor
bac5470881 Collate the OneOfType*/ClassType*/StructType*/ProtocolType* members of
the various NominalDecl subclasses into a single NominalType* member
in NominalDecl. Use it to make TypeDecl::getDeclaredType() more
efficient/simpler, and simplify the ProtocolDecl/ProtocolType
interaction along the way.

No functionality change.


Swift SVN r2298
2012-07-03 21:46:52 +00:00
Doug Gregor
b07b05937a Rework our approach to deduction of generic parameters during overload
resolution. When we see a polymorphic function type, we substitute
"deducible generic parameter" types for each of the generic
parameters. Coercion then deduces those deducible generic parameter
types. This approach eliminates the confusion between the types used
in the definition (which must not be coerced) and the types used when
the generic function is referenced (which need to be coerced).

Note that there are still some terrible inefficiencies in our handling
of these types.



Swift SVN r2297
2012-07-03 21:30:49 +00:00
Eli Friedman
4555345440 Add basic parser/AST support for defining generic types.
Swift SVN r2292
2012-07-03 01:34:58 +00:00
Eli Friedman
a1a182e3d9 Initial implementation of member operators. Known limitations: currently only works for operators on structs/classes, the lookup rules aren't completely settled,
and we don't actually check whether it's reasonable to declare a given operator as a member.



Swift SVN r2277
2012-06-29 00:47:33 +00:00
Doug Gregor
bf71eeb26d Include the generic parameter list of a function declaration in the
FuncDecl AST, and use it for local name lookup.


Swift SVN r2198
2012-06-18 23:49:40 +00:00
Doug Gregor
ec31aaf04b Parse a generic parameter list within a function declaration, and
introduce the generic type parameters (which are simply type aliases
for a to-be-determined archetype type) into scope for name
lookup. We can now parse something like

  func f<T, U : Range>(x : T, y : U) { }

but there is no semantic analysis or even basic safety checking (yet).




Swift SVN r2197
2012-06-18 22:49:54 +00:00
Eli Friedman
2eb6261838 Don't try to capture TypeDecls in local functions.
Swift SVN r2180
2012-06-08 23:51:02 +00:00
Eli Friedman
2880b0afda Address Doug's review comments on constructors.
Swift SVN r2176
2012-06-08 19:59:55 +00:00
Eli Friedman
f1ffa870a3 A few adjustments to AST/Sema for ConstructorDecls, and starting IRGen.
Swift SVN r2160
2012-06-06 00:53:44 +00:00
Eli Friedman
75907029f1 Add parsing and semantic analysis for a basic ConstructorDecl. Still missing: no IRGen, and semantic analysis to actually call them.
Swift SVN r2159
2012-06-05 23:51:19 +00:00
Doug Gregor
9abd2340bc Allow an 'inheritance' clause on typealiases, specifying what
protocols the underlying type of the type alias shall conform to. This
isn't super-motivating by itself (one could extend the underying type
instead), but serves as documentation, makes typealiases provide the
same syntax as other nominal types in this regard, and will also be
used to specify requirements on associated types.


Swift SVN r2133
2012-06-04 16:15:47 +00:00
Doug Gregor
d41e39a363 Sink the 'inherited' members for the various nominal types down into
TypeDecl; TypeAliasDecl will get this ability soon.


Swift SVN r2132
2012-06-04 15:56:29 +00:00
Eli Friedman
ae86d64644 Rename Decl::getLocStart() to Decl::getStartLoc(). Add Decl::getLoc(), which is essentially the location which should be used for diagnostics.
Swift SVN r2105
2012-05-31 23:56:30 +00:00
Eli Friedman
ba4a76038b Make oneofs never implicitly generate an ExtensionDecl. This matters for local oneofs.
Swift SVN r2098
2012-05-31 21:20:56 +00:00
Doug Gregor
3c2fb97bdf Introduce TypeBase::isExistentialType(), to determine whether a given
type is either a protocol type or a protocol composition type. The
long form of this query returns the minimal set of protocol
declarations required by that existential type.

Use the new isExistentialType() everywhere that we previously checked
just for ProtocolType, implementing the appropriate rules. Among other
things, this includes:
  - Type coercion
  - Subtyping relationship
  - Checking of explicit protocol conformance
  - Member name lookup

Note the FIXME for IR generation; we need to decide how we want to
encode the witnesses for the different protocols.

This is most of <rdar://problem/11548207>.


Swift SVN r2086
2012-05-31 00:26:13 +00:00
Eli Friedman
bce8a7a8dc When ValidateType fails on a type, don't keep it around in the AST.
Swift SVN r1927
2012-05-22 01:17:27 +00:00
Eli Friedman
5e206bc10f Rename DistinctTypeDecl to NominalTypeDecl.
Swift SVN r1882
2012-05-17 01:54:15 +00:00