Commit Graph

889 Commits

Author SHA1 Message Date
Doug Gregor
6ad8dfd230 <rdar://problem/13698402> Use raw_ostream::has_colors rather than is_displayed
to determine whether we should display colors.


Swift SVN r4832
2013-04-20 06:55:12 +00:00
Joe Groff
88d5192a69 SILGen: Don't crash if FuncDecls lack bodies.
FuncDecls for builtins don't have bodies. Deal with this when determining function natural uncurry levels and captures for SILGen.

Swift SVN r4734
2013-04-14 19:45:02 +00:00
Joe Groff
d39ee86c71 Sema: Check func operator arity syntactically.
This gives us a couple things:

- It lets name binding match up operator funcs to operator decls reliably without depending on unary operators being properly attributed;
- It allows unary operators on tuples to be distinguished from binary operators; the former should always be declared 'func +(_:(x:A,y:B))', and the latter as 'func +(x:A,y:B)'.

Swift SVN r4636
2013-04-08 18:01:19 +00:00
Joe Groff
ac23437886 Replace infix attributes with operator decl lookup
During name binding, associate func decls with operator decls. When parsing SequenceExprs, look up operator decls to determine associativity and precedence of infix operators. Remove the infix_left and infix_left attributes, and make the infix attribute a simple declared attribute [infix] with no precedence.

Operator decls are resolved as follows:

- If an operator is declared in the same module as the use, resolve to the declaration in the current module.
- Otherwise, import operator declarations from all imported modules. If more than one declaration is imported for the operator and they conflict, raise an ambiguity error. If they are equivalent, pick one arbitrarily.

This allows operator declarations within the current module to override imported declarations if desired or to disambiguate conflicting operator declarations.

I've updated the standard library and the tests. stdlib2 and some of the examples still need to be updated.

Swift SVN r4629
2013-04-07 02:43:03 +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
aeeda4ee12 Parser: Parse operator decls.
At the top level, if 'operator' is followed by 'infix', 'prefix', or 'postfix', consider it a contextual keyword, and parse an operator decl following it that looks like:

  operator {infix|postfix|prefix} <+> {
    attributes…
  }

Prefix and postfix operator decls currently admit no attributes. Infix operators have 'associativity {left|right|none}' and 'precedence <int>' attributes.

This patch implements parsing for operator declarations but does not yet attach the declared attributes to func decls for the operators.

Swift SVN r4596
2013-04-03 23:30:50 +00:00
Joe Groff
8caf747853 Put alignas(N) on classes with strict alignment.
We use three tag bits on Expr*, Stmt*, Decl*, TypeBase* and SILTypeInfo*, and four on DeclContext*, so set the alignment of the pointed-to types formally with alignas(N) instead of relying on operator new passing down the right alignment to the allocator. Get rid of the informal T::Alignment members of these classes and pass alignof(T) to their allocators. Fix the 'operator new' of DeclContext subclasses so that we can actually use the four tag bits PointerLikeTypeTraits<DeclContext*> claims are available.

Swift SVN r4587
2013-04-03 17:27:05 +00:00
Joe Groff
4923acce2b Use C++11 alignof(T) instead of llvm::AlignOf<T>.
Swift SVN r4583
2013-04-03 02:41:29 +00:00
Joe Groff
470ad60d60 IRGen: Emit properties for ivars of ObjC classes.
If a class has ObjC interop, synthesize property accessors for its ivars so that things like IBOutlets and bindings work correctly.

Swift SVN r4240
2013-02-28 20:24:43 +00:00
Joe Groff
be3f4ae1ec IRGen: Emit ObjC methods for properties.
Emit getter and setter ObjC methods and selectors for Swift properties declared in ObjC classes or extensions. If the type of the property is an ObjC class, also emit special-cased ObjC formal property metadata for the property as if it were declared @property (nonatomic, {readonly,strong}) in ObjC.

Swift SVN r4207
2013-02-26 22:57:45 +00:00
Joe Groff
a8b5a2ec26 AST: Add UnresolvedSpecializeExpr.
This node represents a type parameter list application in an unresolved expr context. The type checker will use these to explicitly bind type variables of generic types.

Swift SVN r4046
2013-02-14 19:41:23 +00:00
Doug Gregor
5c26e6cc3d Make sure that the output stream gets flushed before forming the result of getObjCSelector().
Swift SVN r4037
2013-02-13 21:03:11 +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
Joe Groff
750374f61a Sema: Typecheck super.method expressions.
Add support to the constraint checker for typechecking UnresolvedSuperMemberRef expressions and constructing SuperMemberRef or SuperCall expressions as appropriate. We’ll also need a GenericSuperMemberRefExpr to refer to properties of generic supertypes, but in the interests of demo expedience I’m leaving that case partially-implemented for now.

Swift SVN r4020
2013-02-12 19:23:34 +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
Doug Gregor
d17b175a29 When importing an Objective-C class, category, or extension that
explicitly conforms to protocols, mirror any protocol methods that
aren't also declared in the class/category/extension within the
extension. Among other things, this makes sure that "-hash" and
"-isKindOfClass:" are available on NSObject (they come from the
NSObject protocol). 

Fixes <rdar://problem/13074691>.


Swift SVN r3920
2013-02-01 00:44:53 +00:00
Doug Gregor
2b2b2cfc31 Replace the constructor 'alllocates_this' attribute with an 'allocate-this' expression.
By splitting out the expression used to allocate 'this' (which exists
in the AST but cannot be written in the Swift language proper), we
make it possible to emit non-allocating constructors for imported
Objective-C classes, which are the only classes that have an
allocate-this expression.


Swift SVN r3558
2012-12-20 15:28:37 +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
Joe Groff
e43761c199 AST: add print() and dump() methods to Pattern.
Expose the PrintPattern visitor implementation in AST/Decl.cpp through methods on Pattern, as is done with the other AST node types.

Swift SVN r3448
2012-12-12 17:42:15 +00:00
Doug Gregor
f3857df469 Implement an AST pretty-printer for declarations, under -print-ast.
Swift SVN r3431
2012-12-10 23:21:49 +00:00
Joe Groff
ac40cb59ff AST: Dump getter/setter link for FuncDecls.
Swift SVN r3363
2012-12-05 17:59:01 +00:00
Doug Gregor
d04db691ec Retain the first selector piece when importing init/new methods as constructors.
This is the conservative solution that we can revisit later.


Swift SVN r3352
2012-12-04 20:00:01 +00:00
Doug Gregor
629b06623d Import Objective-C categories and extensions as Swift extensions.
Swift SVN r3262
2012-11-27 22:19:16 +00:00
Doug Gregor
311520a3ec Implement support for calling Objective-C instance methods imported from Clang.
Tweak the import of Objective-C methods to build the proper FuncExpr
and tag the FuncDecl as an Objective-C method, along with a few other
tweaks, so calls to the imported Objective-C methods go through
objc_msgSend().

At this moment, this is aborting in the Objective-C runtime due to an
unrecognized selector. The issue does not appear related to the
importer.


Swift SVN r3255
2012-11-27 07:50:52 +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
John McCall
2568c1b043 getSelector -> getObjCSelector per Doug's review.
Swift SVN r3186
2012-11-14 19:30:22 +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
Joe Groff
41a96b71ca Non-settable lvalue checks for TypeCheckConstraints
rdar://11259972
Add a bit to LValueType representing NonSettable, and set it in the constraint-
based checker where a property or subscript expression resolves to a decl
without a setter or to a settable member of an unsettable value type. Tweak
constraint rules for subscript, address-of, assignment, and implicit byref
arguments to rule out operands that are not settable in a context that
requires settability, such as assignment or pass-by-reference.


Swift SVN r3136
2012-11-07 01:47:10 +00:00
John McCall
3989da0acf Add a method for getting the selector string of a method.
Swift SVN r3103
2012-11-03 00:26:55 +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
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
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
b01b27c9c0 A few misc tweaks to -ast-dump for decls.
Swift SVN r2579
2012-08-07 22:52:11 +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
Chris Lattner
51b4f35b9f fix -parse-dump to not crash on pattern_binding_decls that don't have a type yet.
This occurs in "var u8, u8_1 : StringByte"



Swift SVN r2521
2012-08-03 01:18:01 +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
Doug Gregor
e27d65f199 Introduce a "parent' context into oneof, struct, and class types. The
parent context encodes the generic arguments for outer generic types,
so that X<Int>.Nested and X<Float>.Nested are distinct types.


Swift SVN r2502
2012-08-01 23:20:36 +00:00
Doug Gregor
c1229e72a8 Give the methods, constructors, and destructors of a nested type a
polymorphic type when that type is nested within a generic type. The
type-checker still isn't ready to use these declarations in a sane way.



Swift SVN r2501
2012-08-01 21:16:06 +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
Eli Friedman
84e858da4e Fix static member functions so a member of type T has type "metatype<T> -> () -> ()" instead of "() -> ()".
This is much more convenient for IRGen, and gives us a reasonable representation for a static
polymorphic function on a polymorphic type.

I had to hack up irgen::emitArrayInjectionCall a bit to make the rest of this patch work; John, please
revert those bits once emitCallee is fixed.



Swift SVN r2488
2012-07-28 06:47:25 +00:00
Doug Gregor
f4f176dd27 Give the static methods of a generic type polymorphic type,
always. One step closer to representational sanity.


Swift SVN r2477
2012-07-27 07:05:03 +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
Eli Friedman
5b0b5494a2 Update AST dumping for recent change to FuncDecl representation.
Swift SVN r2442
2012-07-25 01:42:11 +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
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