Commit Graph

1894 Commits

Author SHA1 Message Date
Doug Gregor
5034803021 Lazily compute the type of protocol declarations.
Swift SVN r7453
2013-08-22 17:09:16 +00:00
Jordan Rose
95ff29b6e2 Make deserialization of known protocol adopters lazy.
...by adding a new callback to ModuleLoader: loadDeclsConformingTo.
This is used only when the type checker doesn't have enough contextual
information to resolve an expression involving a literal, so it's
possible many *LiteralConvertible types will never be loaded.

Deserialization of types with conversion methods is still eager, since
there's no easy hook to tell when they're needed, but the list has been
renamed to refer to any decls that need to be eagerly deserialized, in
case we need it for other purposes in the future.

This probably won't help much in a real program, but it cuts the test
run time by about 5-10% in my build.

Swift SVN r7268
2013-08-15 18:43:40 +00:00
Jordan Rose
5ce857c45c Only record conformances to known protocols, and include them in modules.
This is really two commits in one: first, change the AST and TypeChecker
to only track conformances to known protocols, and second, make sure we
can deserialize decls that conform to known protocols on demand. The
latter is necessary for the type checker to solve constraint systems that
are not fully constrained, and also requires tracking decls with conversion
methods.

Currently decls conforming to known protocols are eagerly deserialized;
that will change soon to be a new ModuleLoader callback. Decls with
conversion functions will continue to be eagerly deserialized for the near
future.

This fixes the initial regressions in making decl deserialization lazy.

Swift SVN r7264
2013-08-15 17:32:20 +00:00
Jordan Rose
88cad52d72 Implement type sugar "T?" for Optional<T>.
- New type representation OptionalTypeRepr.
- New sugared type OptionalType.
- New base type SyntaxSugarType, parent of ArraySliceType and OptionalType.
  These two are the same in a lot of ways.
- The form "T[]?" is forbidden, because it makes "Int[4][2]" oddly
  different from "Int[4]?[2]". The type can be spelled "(T[])?" or
  Optional<T[]>.
- Like Slice, "Optional" is just looked up in the current module. This may
  or may not be the desired behavior in the long run.

<rdar://problem/14666783>

Swift SVN r7100
2013-08-09 21:33:36 +00:00
Dmitri Hrybenko
d0455ca1c6 Remove unneeded llvm:: qualifier for llvm::ArrayRef
Swift SVN r7093
2013-08-09 20:05:02 +00:00
Dmitri Hrybenko
de59d8dcd4 Remove unneeded llvm:: qualifier for llvm::StringRef and llvm::SmallVector
Swift SVN r7089
2013-08-09 18:41:46 +00:00
John McCall
e9b913fb5b Remove LocalStorageType, make it a kind of SILType.
Swift SVN r6968
2013-08-07 00:22:26 +00:00
John McCall
36aa6c2645 alloc_stack needs to return two values like alloc_box.
The current implementation of dealloc_stack in IR-gen is a
no-op, but that's very much wrong for types with non-trivial
local allocation requirements, e.g. archetypes.  So we need
to be able to do non-trivial code here.  However, that means
modeling both the buffer pointer and the allocated address
in SIL.

To make this more type-safe, introduce a SIL-specific
'[local_storage] T' type that represents the required
allocation for locally storing a T.  alloc_stack now returns
one of those in additon to a *T, and dealloc_stack expects
the former.

IR-gen still implements dealloc_stack as a no-op, but
that's now easy to fix.

Swift SVN r6937
2013-08-06 07:31:41 +00:00
Joe Groff
4316239f5d Kill Builtin.OpaquePointer.
It's not needed by SIL anymore.

Swift SVN r6873
2013-08-03 01:56:06 +00:00
Doug Gregor
666213348d [Protocol conformance] Refactor protocol conformance representation.
Factor the ProtocolConformance class into a small hierarchy of
protocol conformances: 
  - "normal" conformance, which provides a complete mapping for the
  explicit conformance of a nominal type (which may be generic) to a
  protocol;
  -  "specialized" conformance, which specializes a generic
  conformance by applying a set of substitutions; and
  - "inherited" conformance, which projects the conformance from a
  superclass to a conformance for a subclass.

In this scheme "normal" conformances are fairly heavyweight, because
they provide a complete mapping. Normal conformances are unique,
because they're associated with explicit conformance declarations
(which cannot be repeated within a module; checking is TBD). Thus, IR
generation will eventually emit them as strong symbols.

"Specialized" and "inherited" conformances occur when we're dealing
with generic specializations or subclasses. They project most of their
members through to some underlying conformance, eventually landing at
a "normal" conformance. ASTContext is responsible for uniquing these
conformances when it sees them. The IR generation model for
specialized conformances will involve runtime specialization of the
underlying witness table; inherited conformances are probably no-ops
from the IR generation perspective.

Aside from being the right thing to do, having small, uniqued
conformances for the specialization and inheritance cases is good for
compile-time performance and memory usage. We're not really taking
advantage of this everywhere we could, yet.

This change uncovered a few existing issues (one known, one not
known), particularly because we're projecting inherited conformances
rather than building new conformances:
  - <rdar://problem/14620454>: protocol witnesses to methods of
  classes need to perform dynamic dispatch. See the
  test/Interpreter/typeof.swift test for an example.
  - <rdar://problem/14637688>: comparing NSString and String with ==
  fails, because they are inter-convertible. I suspect we were missing
  some protocol conformances previously, and therefore accepting this
  obviously-invalid code.



Swift SVN r6865
2013-08-02 22:59:54 +00:00
Dmitri Hrybenko
e1c4ae3174 Wrap llvm::SourceMgr in swift::SourceManager so that we can add new members
to the source manager.


Swift SVN r6815
2013-08-01 20:39:22 +00:00
Jordan Rose
674a03b085 Replace "oneof" with "union"...everywhere.
We haven't fully updated references to union cases, and enums still are not
their own thing yet, but "oneof" is gone. Long live "union"!

Swift SVN r6783
2013-07-31 21:33:33 +00:00
Doug Gregor
ceaa5e00bf Suggest explicit protocol conformance via Fix-Its.
When we notice that a type implicitly conforms to a protocol but is
not explicitly stated to do so, note this and provide a Fix-It
attaching the conformance to a declaration within the translation
unit, e.g.,

t.swift:28:16: error: type 'S1' does not explicitly conform to protocol 'P'
var p1 : P = S1()
               ^
t.swift:8:8: note: introduce explicit conformance to protocol 'P'
struct S1 : Q {
       ^
             , P



Swift SVN r6760
2013-07-30 22:45:10 +00:00
Jordan Rose
3e7eef56e7 Kill [stdlib] attribute.
Now that we have true serialized modules, the standard library can import
the Builtin module without any special direction (beyond -parse-stdlib),
and anyone can include those modules without special direction.

Swift SVN r6752
2013-07-30 21:27:42 +00:00
Doug Gregor
709882c2b8 Capture complete associated type substitution information in the AST.
Previously, we only tracked the mapping from associated types to their
type witnesses. Now, also track the protocol conformances for each of
the requirements placed on the associated types.


Swift SVN r6655
2013-07-26 22:04:53 +00:00
Doug Gregor
d0f60ab755 Introduce some encapsulation into ProtocolConformance.
Swift SVN r6648
2013-07-26 18:12:17 +00:00
Dmitri Hrybenko
dcd26e8a1f Code completion: implement completion of imported Clang declarations by first
importing them

Because going through the import for every code completion request is slow,
Clang code completion results are cached in the CodeCompletionContext.  The
cache needs to be invalidated whenever a new Clang module is loaded.  In order
to implement this, ModuleLoadListener class was added.


Swift SVN r6505
2013-07-23 18:12:58 +00:00
Anna Zaks
92e07c6a54 Use ExtInfo struct to pass around call related info in FunctionType and PolymorphicFunctionType getters.
Swift SVN r6358
2013-07-18 22:57:21 +00:00
Doug Gregor
acdff12ca0 Unique tuple types that have initializers.
Swift SVN r6355
2013-07-18 22:25:40 +00:00
Doug Gregor
ad1a9ac2c3 Eliminate default argument expressions from the type system.
Elements of a tuple type now know if there is a default argument, and
what kind of default argument it is (callee side, __FILE__, __LINE__,
__COLUMN__), but they don't have an actual expression. There are a
number of cleanups this enables that will follow. 

Note that the serialization support is as-yet-untested.


Swift SVN r6351
2013-07-18 18:49:32 +00:00
Argyrios Kyrtzidis
7476762dae Remove IdentifierType from the type system.
Swift SVN r6327
2013-07-17 14:57:38 +00:00
Argyrios Kyrtzidis
f616eeee8b Utilize TypeReprs for type checking.
-Refactor Parser to stop creating types
-Refactor TypeChecker to create types by resolving TypeReprs.
-Remove "validation" bit from the type system.
  We don't need to "validate" every type that gets created but there's still a validation bit in TypeLoc,
  necessary because of generic substitutions.

Swift SVN r6326
2013-07-17 14:57:35 +00:00
Anna Zaks
98d180c3f3 Pack AnyFunctionType’s extra info inside a separate class stored inside the TypeBits.
Refactored the function calling conventions,"thin" attribute, and other extra function info bits to be stored inside the TypeBits. The isAutoClosure and IsBlock bits are also pulled up into AnyFunctionType.

This should make it easier to construct and propagate the attributes. Using ExtInfo struct to simplify the FunctionType is coming up next.

Swift SVN r6284
2013-07-16 01:50:31 +00:00
Dmitri Hrybenko
aa46064432 Pass a const ASTContext and const DeclContext whenever possible. This makes it
possible to use lookupVisibleDecls() with a const DeclContext.


Swift SVN r6274
2013-07-15 23:39:00 +00:00
Doug Gregor
cf9b8a302e Remove the notion of 'unresolved' types entirely.
Unresolved types are a holdover from the old type checker that not
longer have any purpose in the type system.


Swift SVN r6242
2013-07-13 05:27:22 +00:00
Doug Gregor
d42e385371 Remove UnstructuredUnresolvedType.
Swift SVN r6241
2013-07-13 05:10:38 +00:00
Argyrios Kyrtzidis
b7817e4879 Remove VarargBaseTy field from TupleTypeElt.
Use a bit to indicate whether the element is a vararg one and infer the
VarargBase type via the element type.

Swift SVN r6220
2013-07-12 22:24:31 +00:00
Doug Gregor
9ba865e852 Diagnose ambiguity type-checks by pointing to candidates.
Introduces very basic support for diagnosing ambiguous expressions,
where the source of the ambiguity is a reference to an overloaded
name. Simple example:

t.swift:4:1: error: ambiguous use of 'f0'
f0(1, 2)
^
t.swift:1:6: note: found this candidate
func f0(i : Int, d : Double) {}
     ^
t.swift:2:6: note: found this candidate
func f0(d : Double, i : Int) {}
     ^

There's a lot of work needed to make this cleaner, but perhaps it will
ease the pain of developing the library <rdar://problem/14277889>.


Swift SVN r6195
2013-07-12 00:45:17 +00:00
John McCall
18a9290cbe Add ReferenceStorageType.
The idea for now is that this is a SIL-only type used for
representing the storage of a weak or unowned reference.
Having it be its own type is pretty vital for reasonable
behavior in SIL and IR-generation, and it's likely that
this will surface into runtime metadata as well (hence
the mangling).

I've implemented a bunch of things that technically I don't
think are necessary if this stays out of the typechecker,
but it's easier to implement half-a-dozen "recurse into
the child type" methods now that it would be to find them
all later if we change our minds.

Swift SVN r6091
2013-07-09 08:37:40 +00:00
Doug Gregor
4554961979 [Name lookup] Introduce a lookup table into each nominal type declaration.
The lookup table for a nominal type declaration provides efficient
(O(1)) access to all of the declarations with a given name in a
nominal type and its extensions. This is architecturally different
from Clang's handling of Objective-C classes and
categories/extensions, where each category/extension has its own
lookup table, and is meant to reduce the number of hash table lookups
required, especially once these hash tables are stored in the module.

The lookup table is built and updated lazily as extensions and members
are introduced, similarly to Clang's lookup tables. However, the
simpler name lookup rules in Swift (vs. C/C++/Objective-C) make this
approach actually semantically correct.



Swift SVN r5874
2013-06-28 18:43:41 +00:00
Chris Lattner
c03d4454a0 implement support for a new [stdlib] attribute that can be slapped on an import decl.
This causes the SourceLoader to recursively parse the imported module in standard 
library mode, giving it access to the Builtin module.

This is all a terrible hack and should be ripped out with great victory someday, but 
until we have binary modules that persist the build setting used to produce the 
module, this is the best we can do.



Swift SVN r5847
2013-06-27 21:31:15 +00:00
Doug Gregor
9a5c96a8c1 Introduce basic support for LLVM vectors as builtins.
This adds builtin types Builtin.VecNxT, where N is a natural number
and T is a builtin type, which map down to the LLVM type <N x T>. 

Update varous builtins to support vector arguments, e.g., binary
operations, comparisons, negation. Add InsertElement and
ExtractElement builtins for vectors.

On top of these builtins, add Vec4f and Vec4b structs to the standard
library, which provide 4xFloat and 4xBool vectors, respectively, with
basic support for arithmetic. These are mostly straw men, to be burned
down at our leisure.

Some issues as yet unresolved:
  - Comparisons of Vec4f'ss are producing bogus Vec4b's, which I
  haven't tracked down yet.
  - We still don't support the shuffle builtin, although it should be
  easy
  - More testing!



Swift SVN r5820
2013-06-26 21:16:36 +00:00
Chris Lattner
95163ff152 introduce a new TranslationUnit kind for .sil files, and use this to
guard access to the Builtin module (on one path), reducing the number
of ways non-stdlib and non-sil files have access to the builtin module.


Swift SVN r5767
2013-06-22 17:14:18 +00:00
Doug Gregor
287da5676e [Constraint solver] Explore types that conform to each required protocol.
Keep track of each of the nominal type declarations and extensions
thereof that conform to each protocol. When the type checker runs out
of other ideas for a type variable, enumerate the types known to
conform to the protocols it requires. Fixes <rdar://problem/14014895>
and eliminates the extra casts introduced in r5639.


Swift SVN r5645
2013-06-18 16:46:04 +00:00
Doug Gregor
8114ce16f8 Use the list of extensions of a nominal type for name lookup into that type.
This replaces the obscure, inefficient lookup into extensions with
something more straightforward: walk all of the known extensions
(available as a simple list), then eliminate any declarations that
have been shadowed by other declarations. The shadowing rules still
need to consider the module re-export DAG, but we'll leave that for
later.

As part of this, keep track of the last time we loaded extensions for
a given nominal type. If the list of extensions is out-of-date with
respect to the global generation count (which tracks resolved module
imports), ask the modules to load any additional extensions. Only the
Clang module importer can currently load extensions in this manner.


Swift SVN r5223
2013-05-20 18:06:51 +00:00
Joe Groff
a4ce448280 Move cc attribute from SILType to AnyFunctionType.
We decided we're going to want to surface fine-grained representational control of functions to the user, so move AbstractCC and the calling convention attributes into the Swift type system. Like the [thin] attribute, we don't set this in the type-checker or importer at all yet, and let SILGen set the attribute where it wants it for now.

Swift SVN r5222
2013-05-20 17:55:51 +00:00
Joe Groff
e1c838962e Revert "Remove [objc_block] attribute from Swift type system."
Implementing SIL bridging is going to take more IRGen work than I anticipated.

Swift SVN r5113
2013-05-09 16:32:18 +00:00
Jordan Rose
ccae995f61 Use a list of module loaders instead of a single Clang importer.
This paves the way for having a Swift module importer. The eventual goal
here is to eliminate all explicit uses of the Clang module loader, but
I'm not going to push too hard on that for now.

Swift SVN r5092
2013-05-08 18:09:33 +00:00
Joe Groff
38f13e56f5 Remove [objc_block] attribute from Swift type system.
We will handle Swift-function-to-ObjC-block bridging in SILGen as part of general Cocoa-to-Swift type bridging. Temporarily disable building swiftAppKit and tests that exercise block bridging until the new implementation lands.

Swift SVN r5090
2013-05-08 16:52:12 +00:00
Joe Groff
c75e1bb048 Sema: Correctly set parent type of local types looked up in context.
When an IdentifierType is resolved to a local type in the same decl context, we weren't setting the parent type correctly, causing blowups in type checking when a local type didn't have its generic parameters from context available. Set the parent type to the DeclaredTypeInContext of the type to which we resolved an unqualified lookup. Fixes <rdar://problem/12895793>.

Swift SVN r5084
2013-05-08 00:34:36 +00:00
Doug Gregor
0f6b7a9d22 Rework our handling of "external" definitions created by the Clang importer.
Keep track of external definitions as they are created by broadcasting
them through a mutation listener interface. At name binding time, we
just cache them. When a type checker is alive, it immediately performs
any additional operations necessary on those types (e.g., declaring
implicit constructors).

This also eliminates some O(N^2) behavior in the type checker as well,
because we don't have to walk through all of the module imports to
find the external definitions. We just keep a single list in the
ASTContext along with our place in the list.

Fixes <rdar://problem/13769497>.


Swift SVN r5032
2013-05-03 00:24:34 +00:00
Joe Groff
aa6c506f58 Add '[thin]' attribute to function types.
Some SIL operations only make sense on no-context functions, such as creating a context for a function with "curry" or specializing. We're going to need this concept in Swift for interop with C functions too, so I'm adding it to the Swift type system. This patch only adds the attribute bit to function types without exposing the attribute to Swift syntax or providing any means in Swift to produce values of thin function type or to type-check them.

Swift SVN r4416
2013-03-18 19:59:39 +00:00
Doug Gregor
a9a90fd56a Track the Clang macros that were used to generate Swift constants.
Swift SVN r3960
2013-02-06 00:57:03 +00:00
Joe Groff
30e715f633 AST: Hack in an '[objc_block]' FunctionType attr.
For the demo we'll import block types as [objc_block] (A) -> B types in Swift and rely on the type attribute to handle bridging Swift closures to ObjC blocks.

Swift SVN r3895
2013-01-29 22:38:21 +00:00
Joe Groff
0422897a47 Expose %swift.opaque as a builtin type.
Archetypes and projected existentials have the type %swift.opaque* and not i8*, so I need a corresponding SIL type to be able to model the ProjectExistential operation. We might also end up needing the builtin type for other low-level things down the line.

Swift SVN r3793
2013-01-18 02:24:23 +00:00
Doug Gregor
83f2874ba1 Implement support for calling imported C functions from Swift.
There is no protection whatsoever if the Clang-to-Swift type
conversion produces something that Swift doesn't lower in an
ABI-compatible way. That will be dealt with later.


Swift SVN r3249
2012-11-27 00:34:48 +00:00
Doug Gregor
bb26f52585 Initial support for loading Clang modules into Swift.
From a user's perspective, one imports Clang modules using the normal
Swift syntax for module imports, e.g.,

  import Cocoa

However, to enable importing Clang modules, one needs to point Swift
at a particular SDK with the -sdk= argument, e.g.,

  swift -sdk=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9M.sdk

and, of course, that SDK needs to provide support for modules.

There are a number of moving parts here. The major pieces are:

CMake support for linking Clang into Swift: CMake users will now need
to set the SWIFT_PATH_TO_CLANG_SOURCE and SWIFT_PATH_TO_CLANG_BUILD
to the locations of the Clang source tree (which defaults to
tools/clang under your LLVM source tree) and the Clang build tree.

Makefile support for linking Clang into Swift: Makefile users will
need to have Clang located in tools/clang and Swift located in
tools/swift, and builds should just work.

Module loader abstraction: similar to Clang's module loader,
a module loader is responsible for resolving a module name to an
actual module, loading that module in the process. It will also be
responsible for performing name lookup into that module.

Clang importer: the only implementation of the module loader
abstraction, the importer creates a Clang compiler instance capable of
building and loading Clang modules. The approach we take here is to
parse a dummy .m file in Objective-C ARC mode with modules enabled,
but never tear down that compilation unit. Then, when we get a request
to import a Clang module, we turn that into a module-load request to
Clang's module loader, which will build an appropriate module
on-the-fly or used a cached module file.

Note that name lookup into Clang modules is not yet
implemented. That's the next major step.



Swift SVN r3199
2012-11-16 18:17:05 +00:00
Doug Gregor
2183b57fa4 Introduce a special memory arena in ASTContext for the type checker.
This introduces the notion of arenas into ASTContext, with two arenas
currently defined: one for 'permanent' storage, and another for the
current constraint checker. The latter is used when allocating any
types that involve type variables, which are only used temporarily
during type checking anyway.

This gives us a 1% speedup on swift.swift (because we're hitting
smaller hash tables when doing lookups) and < 1% memory reduction
(since that's not the main source of memory usage). It's more
important architecturally, so our memory usage doesn't grow with the
number of type-checks performed.

Note also that this arena scheme could be generalized, which we may
very well want to do in the future. For example, we could easily have
an arena for temporary nodes introduced by parsing (e.g.,
UnresolvedDeclRefExpr) or by name binding (OverloadedDeclRefExpr), and
clear that arena when we successfully move onto the next phase. Or, in
a REPL/debugger context, have a 'temporary' arena for
statements/expressions that can be removed.



Swift SVN r3175
2012-11-14 06:57:16 +00:00
John McCall
24d819e133 Split BoundGenericType into BoundGeneric{Class,OneOf,Struct}Type.
We'll want a superclass pointer on ClassType and BoundGenericClassType,
and this also makes it easier to detect both kinds of class type.

Swift SVN r3061
2012-10-25 21:44:44 +00:00
Eli Friedman
77c0114186 Make sure TupleType::get never returns an invalid TupleType.
Finishes off <rdar://problem/12337042>.

Also, fix constraint application so that test/Constraints/closures.swift
doesn't explode with the above fix.



Swift SVN r2888
2012-09-21 01:54:26 +00:00