Commit Graph

1018 Commits

Author SHA1 Message Date
Jordan Rose
9156e805f5 Don't crash when looking for imports in a loaded module.
At some point this will change because of re-exported modules, but for now
just don't crash when performing unqualified lookup on a loaded module.

Also, auto-load our adapter libraries for loaded modules, not just TUs.
Include "POSIX" in this list. (We really need autolinking.)

Swift SVN r6303
2013-07-16 23:10:49 +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
Joe Groff
3b21e6a201 Add a OneOfElementPattern AST node type.
Add a node for references to oneof cases in patterns. No user-facing functionality change yet.

Swift SVN r6239
2013-07-13 01:21:31 +00:00
Doug Gregor
773153c84b Don't allow generic parameters to be found via qualified name lookup.
This was an oddity in qualified name lookup that appears to be a
workaround for missing behavior in unqualified name
lookup. Unqualified name lookup is still begging some serious
cleanup.


Swift SVN r6076
2013-07-08 23:23:55 +00:00
Doug Gregor
fc1c256ef5 Allow references to deduced associated types.
When checking a type's conformance against a protocol, we can deduce
the values of associated types. Make these associated types visible to
qualified name lookup so that (for example) VectorEnumeratorType does
not need to define the Element type. It is deduced from the signautre
of next(), and made available as, e.g.,
VectorEnumeratorType<Int>.Element through the Enumerator protocol
conformance. Fixes <rdar://problem/11510701>, but with some lingering
dependencies on lazy type resolution (<rdar://problem/12202655>).

Note that the infrastructure here is meant to be generalized to
support default implementations in protocols, but there are several
pieces still not in place.



Swift SVN r6073
2013-07-08 22:32:27 +00:00
Doug Gregor
1239757489 [Name lookup] Move ASTContext::lookup to Module::lookupQualified, where it belongs.
No functionality change.


Swift SVN r5921
2013-07-01 14:05:55 +00:00
Doug Gregor
a99df7a7b9 [Name lookup] Eliminate MemberLookup in favor of ASTContext::lookup().
Swift SVN r5888
2013-06-28 22:56:15 +00:00
Doug Gregor
187f80cf60 [Name lookup] Introduce ASTContext::lookup() for name lookup into a type.
This lookup routine takes the place of MemberLookup for AST-level
lookups, which don't consider semantics at all and won't be able to
(for example) perform additional type checking to resolve the
lookup. No functionality change.


Swift SVN r5882
2013-06-28 22:08:42 +00:00
Doug Gregor
2994801448 [Name lookup] Eliminate ConstructorLookup, inlining its behavior
This also eliminates some support routines that are no longer needed
with the new member lookup code.


Swift SVN r5878
2013-06-28 20:38:59 +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
Doug Gregor
90d0cf0614 [Name lookup] Make MemberLookup stop filtering out static methods when looking into a type.
That one cannot call a static method is a semantic restriction, not a
behavior of name lookup, and name lookup shouldn't play Sema.


Swift SVN r5869
2013-06-28 15:28:39 +00:00
Joe Groff
53221db84c AST: Add 'VarPattern' node.
We decided to go with 'var' as a distributive pattern introducer which applies to bare identifiers within the subpattern. For example, 'var (a, b)' and '(var a, var b)' would be equivalent patterns. To model this, give 'var' its own AST node with a subpattern and remove the introducer loc from NamedPattern.

Swift SVN r5824
2013-06-26 23:01:47 +00:00
Joe Groff
e460a01af6 Remove the 'UnresolvedCallPattern' I stubbed out.
I talked to John about parsing patterns today, and because of the magnitude of name-lookup-dependent ambiguities between patterns and expressions, we agreed that at least for a first-pass implementation it makes sense to parse patterns as extensions of the expr grammar and charge name binding with distinguishing patterns from expressions. This gets us out of needing the concept of an "unresolved pattern", at least in the short term.

Swift SVN r5808
2013-06-26 04:23:47 +00:00
Joe Groff
7ba95cfd26 Add AST nodes for refutable patterns.
Introduce Pattern subclasses for the 'is T', 'T(<pattern>)', and '<expr>' pattern syntaxes we'll be introducing for pattern-matching "switch" statements. Also add an 'UnresolvedCalLPattern' to act as an intermediate for name lookup to resolve to a nominal type, oneof element, or function call expression pattern. Since we'll need to be able to rewrite patterns like we do expressions, add setters to AST nodes that contain references to subpatterns. Implement some basic walking logic in places we search patterns for var decls, but punt on any more complex type-checking or SILGen derived from these nodes until we actually use them.

Swift SVN r5780
2013-06-24 17:17:34 +00:00
Dmitri Hrybenko
8c3995996f Add infrastructure to easily map C types to swift stdlib types when they are
binary compatible.

This enables us to import MacTypes.h types as stdlib swift types to avoid name
conflicts.  We also import stdint.h types as stdlib swift types.

We can not map 'long double'-based ctypes.Float80 to swift.Float80 because size
of long double is 128 according to SysV ABI, and I compare that against what
the type name says (I did not find a way to find the size of the type).

Darwin.Float80 is a struct, so I don’t think we can import it as swift.Float80.
So with import Darwin, Float80 is still ambiguous.

I had to rename test/ClangModules/ctypes.swift ->
test/ClangModules/ctypes_test.swift because other tests do 'import ctypes',
which picks up 'ctypes.swift'.


Swift SVN r5727
2013-06-20 22:08:35 +00:00
Dmitri Hrybenko
7bf6d30b62 Disambiguate imported functions and structs that have the same name.
For example, now we disambiguate 'stat()' function and 'struct stat' from
the Darwin module.


Swift SVN r5587
2013-06-14 21:12:46 +00:00
Joe Groff
cb1f81db84 Make assignment an expression.
Change AssignStmt into AssignExpr; this will make assignment behave more consistently with assignment-like operators, and is a first step toward integrating '=' parsing with SequenceExpr resolution so that '=' can obey precedence rules. This also nicely simplifies the AST representation of c-style ForStmts; the initializer and increment need only be Expr* instead of awkward Expr*/AssignStmt* unions.

This doesn't actually change any user-visible behavior yet; AssignExpr is still only parsed at statement scope, and typeCheckAssignment is still segregrated from the constraint checker at large. (In particular, a PipeClosureExpr containing a single assign expr in its body still doesn't use the assign expr to resolve its own type.) The parsing issue will be addressed by handling '=' during SequenceExpr resolution. typeCheckAssignment can hopefully be reworked to work within the constraint checker too.

Swift SVN r5500
2013-06-06 22:18:54 +00:00
Joe Groff
8693c5efaa SIL: Emit String-to-NSString conversions.
If -nsstring-is-string is enabled, lower Strings in cc(c) and cc(objc) function types to NSString, and when calling them, insert calls to StringToNSString/NSStringToString to perform the bridging conversion.

This isn't quite ready for prime-time yet, because we still need to emit the inverse bridging for ObjC method thunks, and I haven't tested the IRGen end of things yet.

Swift SVN r5355
2013-05-26 20:29:09 +00:00
Doug Gregor
cbb3c2dff4 TinyPtrVector is better than SmallVector for the expected case (no shadowing).
Swift SVN r5228
2013-05-20 18:39:48 +00:00
Doug Gregor
e20338398e Switch visible-declaration lookup to walking extension lists.
No functionality change, but this is a win for performance and
clarity.


Swift SVN r5224
2013-05-20 18:17:25 +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
Doug Gregor
ce3fe3ae92 Implement Ruby-inspired closure syntax.
This commit implements closure syntax that places the (optional)
parameter list in pipes within the curly braces of a closure. This
syntax "slides" well from very simple closures with anonymous
arguments, e.g.,

  sort(array, {$1 > $0})

to naming the arguments

  sort(array, {|x, y| x > y})

to adding a return type and/or parameter types

  sort(array, {|x : String, y : String| -> Bool x > y})

and with multiple statements in the body:

  sort(array, {|x, y|
    print("Comparing \(x) and \(y)\n")
    return x > y
  })

When the body contains only a single expression, that expression
participates in type inference with its enclosing expression, which
allows one to type-check, e.g.,

  map(strings, {|x| x.toUpper()})

without context. If one has multiple statements, however, one will
need to provide additional type information either with context

  strings = map(strings, {
    return $0.toUpper()
  })

or via annotations

  map(strings, {|x| -> String 
    return x.toUpper()
  }

because we don't perform inter-statement type inference.

The new closure expressions are only available with the new type
checker, where they completely displace the existing { $0 + $1 }
anonymous closures. 'func' expressions remain unchanged.

The tiny test changes (in SIL output and the constraint-checker test)
are due to the PipeClosureExpr AST storing anonymous closure arguments
($0, $1, etc.) within a pattern in the AST. It's far cleaner to
implement this way.

The testing here is still fairly light. In particular, we need better
testing of parser recovery, name lookup for closures with local types,
more deduction scenarios, and multi-statement closures (which don't
get exercised beyond the unit tests).



Swift SVN r5169
2013-05-14 05:17:10 +00:00
Joe Groff
bfd2f85b5c Parse 'fallthrough' statements.
Create a new FallthroughStmt, which transfers control from a 'case' or 'default' block to the next 'case' or 'default' block within a switch. Implement parsing and sema for FallthroughStmt, which syntactically consists of a single 'fallthrough' keyword. Sema verifies that 'fallthrough' actually appears inside a switch statement and that there is a following case or default block to pass control to.

SILGen/IRGen support forthcoming.

Swift SVN r4653
2013-04-10 17:30:42 +00:00
Chris Lattner
b4fd6dd04a Change TopLevelCodeDecl to allow it to hold a sequence of different exprs and statements in one unit, wrapping them into a BraceStmt. This makes it more similar to other decls (e.g. funcdecl, ctor decls, etc) and will be useful for future sil work.
Unfortunately, this regresses the repl when expressions like (1,2) are entered. This is because the repl is violating some invariants (forming dags out of ASTs, making ASDAG's which upset the type checker).  I'm going to fix this next, but can't bring myself to do it in the same commit.



Swift SVN r4617
2013-04-05 22:33:14 +00:00
Joe Groff
062ad267c4 Value-only switch statements.
Implement switch statements with simple value comparison to get the drudge work of parsing and generating switches in place. Cases are checked using a '=~' operator to compare the subject of the switch to the value in the case. Unlike a C switch, cases each have their own scope and don't fall through. 'break' and 'continue' apply to an outer loop rather to the switch itself. Multiple case values can be specified in a comma-separated list, as in 'case 1, 2, 3, 4:'. Currently no effort is made to check for duplicate cases or to rank cases by match strength; cases are just checked in source order, and the first one wins (aside from 'default', which is branched to if all cases fail).

Swift SVN r4359
2013-03-12 04:43:01 +00:00
Doug Gregor
b660580f35 Don't permit calling static methods on an instance <rdar://problem/13206762>.
Swift SVN r4033
2013-02-13 19:27:39 +00:00
Dave Zarzycki
2f31759280 Remove SemiStmt class
We have no intention of ever supporting actual semicolon statements
(separators, statements no), nor do we ever want to because that would
mean the behavior of the program would potentially change if semicolons
were naively removed.

This patch tracks the trailing semicolon now in the decl/expr/stmt,
which will enable someone to write a good "swift indent" tool in the
future.

Swift SVN r3824
2013-01-22 00:25:26 +00:00
Doug Gregor
75c8591487 When lookup finds a generic parameter, treat it like a local declaration, not a member declaration.
Swift SVN r3534
2012-12-18 22:50:49 +00:00
Doug Gregor
bfcd8274fe Every Swift file is a module; let name lookup find the name of the current module.
Swift SVN r3531
2012-12-18 21:41:58 +00:00
Doug Gregor
71a7408d13 Teach name lookup to remove overridden methods in extensions from its results.
While we haven't worked out the details of whether methods in
extensions can be overridden in Swift, it's something that does happen
in Objective-C, so we need to deal with it.

With this change, note that our demo application can both allocate
Objective-C objects with "new" (which John recently fixed) and also
subscript mutable arrays to both read and write.


Swift SVN r3485
2012-12-13 23:19:37 +00:00
Doug Gregor
be915b9c04 Implement semantic analysis for inherits-from constraints.
This change enables inheritance constraints such as "T : NSObject",
which specifies that the type parameter T must inherit (directly or
indirectly) from NSObject. One can then implicit convert from T to
NSObject and perform (checked) downcasts from an NSObject to a T. With
this, we can type-

IR generation still needs to be updated to handle these implicit
conversions and downcasts. New AST nodes may follow.


Swift SVN r3459
2012-12-12 23:28:19 +00:00
Doug Gregor
e15f75f7e3 Support importing multiple Clang modules into Swift.
This is mostly a hack to work around differences between how Swift and
Clang name lookup into modules works. However, it allows us to load
multiple Clang modules into Swift without causing spurious
ambiguities. The generation-based versioning isn't stricly necessary,
since module imports are resolved up front. However, we may eventually
want to speculatively load modules as part of name binding or type
checking, in which case we'd rather not have stale caches. And it
costs us very little.


Swift SVN r3269
2012-11-27 22:47:31 +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
Joe Groff
6449655e21 Implement selector-style function definition syntax.
rdar://12315571
Allow a function to be defined with this syntax:

  func doThing(a:Thing) withItem(b:Item) -> Result { ... }

This allows the keyword names in the function type (in this case
`(_:Thing, withItem:Item) -> Result`) to differ from the names bound in the
function body (in this case `(a:Thing, b:Item) -> Result`, which allows
for Cocoa-style `verbingNoun` keyword idioms to be used without requiring
those keywords to also be used as awkward variable names. In addition
to modifying the parser, this patch extends the FuncExpr type by replacing
the former `getParamPatterns` accessor with separate `getArgParamPatterns`
and `getBodyParamPatterns`, which retrieve the argument name patterns and
body parameter binding patterns respectively.



Swift SVN r3098
2012-11-01 21:53:15 +00:00
Ted Kremenek
a5ec0af7e8 Rename 'BraceStmt::elements()' to 'getElements()' to match Swift
naming style for accessors.

Swift SVN r2742
2012-08-24 14:17:28 +00:00
Eli Friedman
eb1689710f Delete isa, cast, and dyn_cast on Type. Hopefully, this will lead to fewer stupid mistakes.
Swift SVN r2691
2012-08-20 22:15:10 +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
Eli Friedman
6781f56cfd Start of support for class inheritance.
Swift SVN r2598
2012-08-09 21:56:05 +00:00
Ted Kremenek
479077e354 Remove individual element setters from BraceStmt, and just use MutableArrayRef and ArrayRef to access its elements.
Swift SVN r2586
2012-08-08 05:06:33 +00:00
Doug Gregor
4d4ab92859 When unqualified name lookup from an instance method, constructor, or
destructor finds a metatype member (e.g., a typealias), the resulting
access expression should be based on the metatype (possibly
specialized) rather than based on 'this'. Fixes <rdar://problem/11926972>.


Swift SVN r2390
2012-07-22 07:39:42 +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
c9cb594eb3 IRGen for destructors. (This code is likely to change a lot once we design classes properly, but it should be a decent start.)
Swift SVN r2365
2012-07-17 22:41:23 +00:00
Eli Friedman
908cf4573f A couple minor tweaks to get extensions for generic types working.
Swift SVN r2335
2012-07-10 22:19:53 +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
6258942b30 Explicitly specify whether we're performing type lookup to the name lookup code, rather than deducing it from the translation phase.
Swift SVN r2312
2012-07-06 01:06:46 +00:00
Eli Friedman
a65b737730 Allow name lookup into generic types.
Swift SVN r2305
2012-07-04 02:36:45 +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
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
827e87cac3 Switch the representation of the list of protocols to which an
archetype conforms from a list of Types (each of which is existential)
to the minimized, sorted list of protocol declarations as used by
canonical protocol composition types. This stable list is more
suitable for IR generation.


Swift SVN r2238
2012-06-25 18:37:52 +00:00