This is the language reference manual for the Swift language, which is highly volatile and constantly under development. As the prototype evolves, this document should be kept up to date with what is actually implemented.
The grammar and structure of the language is defined in BNF form in yellow boxes. Examples are shown in gray boxes, and assume that the standard library is in use (unless otherwise specified).
In no particular order, and not explained well:
A smaller wishlist goal is to support embedded sub-languages in swift, so that we don't get the OpenCL-is-like-C-but-very-different-in-many-details problem.
The basic approach in designing and implementing the Swift prototype was to start at the very bottom of the stack (simple expressions and the trivial bits of the type system) and incrementally build things up one brick at a time. There is a big focus on making things as simple as possible and having a clean internal core. Where it makes sense, sugar is added on top to make the core more expressive for common situations.
One major aspect that dovetails with expressivity, learnability, and focus on API development is that much of the language is implemented in a standard library (inspired in part by the Haskell Standard Prelude). This means that things like 'Int' and 'Void' are not part of the language itself, but are instead part of the standard library.
Swift has a strict separation between its phases of translation, and the compiler follows a conceptually simple design. The phases of translation are:
FIXME: "import swift" implicitly added as the last import in translation unit.
The lexical structure of a Swift file is very simple: the files are tokenized according to the following productions and categories. As is usual with most languages, tokenization uses the maximal munch rule and whitespace separates tokens. This means that "a b" and "ab" lex into different token streams and are therefore different in the grammar.
whitespace ::= ' '
whitespace ::= '\n'
whitespace ::= '\r'
whitespace ::= '\t'
whitespace ::= '\0'
comment ::= //.*[\n\r]
comment ::= /* .... */
Space, newline, tab, and the nul byte are all considered whitespace and are discarded, with one exception: a '(' or '[' which does not follow a non-whitespace character is different kind of token (called spaced) from one which does not (called unspaced). A '(' or '[' at the beginning of a file is spaced.
Comments may follow the BCPL style, starting with a "//" and running to the end of the line, or may be recursively nested /**/ style comments. Comments are ignored and treated as whitespace.
lparen-spaced ::= '(' // preceded by space
lparen-unspaced ::= '(' // not preceded by space
lparen-any ::= lparen-spaced
lparen-any ::= lparen-unspaced
lsquare-spaced ::= '[' // preceded by space
lsquare-unspaced ::= '[' // not preceded by space
lsquare-any ::= lsquare-spaced
lsquare-any ::= lsquare-unspaced
punctuation ::= lparen-spaced
punctuation ::= lparen-unspaced
punctuation ::= ')'
punctuation ::= '{'
punctuation ::= '}'
punctuation ::= lsquare-spaced
punctuation ::= lsquare-unspaced
punctuation ::= ']'
punctuation ::= '.'
punctuation ::= ','
punctuation ::= ';'
punctuation ::= ':'
punctuation ::= '='
punctuation ::= '->'
punctuation ::= '...'
These are all reserved punctuation that are lexed into tokens. Most other punctuation is matched as identifiers.
keyword ::= 'do'
keyword ::= 'else'
keyword ::= 'extension'
keyword ::= 'if'
keyword ::= 'import'
keyword ::= 'in'
keyword ::= 'for'
keyword ::= 'func'
keyword ::= 'oneof'
keyword ::= 'static'
keyword ::= 'protocol'
keyword ::= 'return'
keyword ::= 'struct'
keyword ::= 'typealias'
keyword ::= 'var'
keyword ::= 'while'
These are the builtin keywords.
integer_literal ::= [0-9]+
integer_literal ::= 0x[0-9a-fA-F]+
integer_literal ::= 0o[0-7]+
integer_literal ::= 0b[01]+
integer_literal tokens represent simple integer values of unspecified precision.
floating_literal ::= [0-9]+\.[0-9]+
floating_literal ::= [0-9]+(\.[0-9]*)?[eE][+-][0-9]+
floating_literal ::= \.[0-9]+([eE][+-][0-9]+)?
floating_literal tokens represent floating point values of unspecified precision.
character_literal ::= '[^"\\\n\r]|character_escape'
character_escape ::= [\][\] | [\]t | [\]n | [\]r | [\]" | [\]'
character_escape ::= [\]x hex hex
character_escape ::= [\]u hex hex hex hex
character_escape ::= [\]U hex hex hex hex hex hex hex hex
hex ::= [0-9a-fA-F]
character_literal tokens represent a single character, and are surrounded by single quotes.
string_literal ::= ["]([^"\\\n\r]|character_escape|escape_expr)*["]
escape_expr ::= [\]escape_expr_body
escape_expr_body ::= [(]escape_expr_body[)]
escape_expr_body ::= [^\n\r"()]
string_literal tokens represent a string, and are surrounded by double quotes. String literals cannot span multiple lines.
String literals may contain embedded expressions in them (known as "interpolated expressions") subject to some specific lexical constraints: the expression may not contain a double quote ["], newline [\n], or carriage return [\r]. All parentheses must be balanced.
In addition to these lexical rules, an interpolated expression must satisfy the expr production of the general swift grammar. This expression is evaluated, and passed to the constructor for the inferred type of the string literal. It is concatenated onto any fixed portions of the string literal with a global "+" operator that is found through normal name lookup.
// Simple string literal.
"Hello world!"
// Interpolated expressions.
"\(min)..\(max)" + "Result is \((4+i)*j)"
identifier ::= [a-zA-Z_][a-zA-Z_$0-9]*
operator ::= [/=-+*%<>!&|^~]+
operator ::= \.\.
Note: excludes '=', see [1]
excludes '->', see [2]
'..' is an operator, not two '.'s.
operator-binary ::= operator
operator-prefix ::= operator
operator-postfix ::= operator
left-binder ::= [ \r\n\t\(\[\{,;]
any-identifier ::= identifier | operator
operator-binary, operator-prefix, and operator-postfix are distinguished by immediate lexical context. An operator token is called left-bound if it is immediately preceded by a character matching left-binder. An operator token is called right-bound if it is immediately followed by a character matching right-binder. An operator token is an operator-prefix if it is right-bound but not left-bound, an operator-postfix if it is left-bound but not right-bound, and an operator-binary in either of the other two cases.
When parsing certain grammatical constructs that involve '<' and
'>' (such as protocol composition
types), an operator with a leading '<' or '>' may
be split into two or more tokens: the leading '<' or '>' and
the remainder of the token, which may be an operator or
punctuation token that may itself be further split. This
rule allows us to parse nested constructs such as
A<B<C>> without requiring spaces between
the closing '>'s.
dollarident ::= $[0-9a-zA-Z_$]*
Tokens that start with a $ are separate class of identifier, which are fixed purpose names that are defined by the implementation.
decl ::= decl-class
decl ::= decl-constructor
decl ::= decl-destructor
decl ::= decl-extension
decl ::= decl-func
decl ::= decl-import
decl ::= decl-oneof
decl ::= decl-protocol
decl ::= decl-struct
decl ::= decl-typealias
decl ::= decl-var
decl ::= decl-subscript
translation-unit ::= stmt-brace-item*
The top level of a swift source file is grammatically identical to the contents of a brace statement. Some declarations have semantic restrictions that only allow them within a translation unit though.
decl-import ::= 'import' attribute-list any-identifier ('.' any-identifier)*
'import' declarations allow named values and types to be accessed with local names, even when they are defined in other modules and namespaces. See the section on name binding for more information on how these work. import declarations are only allowed at translation unit scope.
'import' directives only impact a single translation unit: imports in one swift file do not affect name lookup in another file. import directives can only occur at the top level of a file, not within a function or namespace.
If a single identifier is specified for the import declaration, then the entire module is imported in its entirety into the current scope. If a scope (such as a namespace) is named, then all entities in the namespace are imported. If a specific type or variable is named (e.g. "import swift.Int") then only the one type and/or value is imported. If the named value is overloaded, then the entire overload set is imported.
// Import all of the top level symbols and types in a package.
import swift
// Import all of the symbols within a namespace.
import swift.io
// Import a single variable, function, type, etc.
import swift.io.bufferedstream
// Import all multiplication overloads.
import swift.*
decl-extension ::= 'extension' type-identifier inheritance? '{' decl* '}'
'extension' declarations allow adding member declarations to existing types, even in other translation units and modules. There are different semantic rules for each type that is extended.
FIXME: Write this section.
decl-var ::= 'var' attribute-list pattern initializer? (',' pattern initializer?)*
decl-var ::= 'var' attribute-list identifier ':' type-annotation '{' get-set '}'
initializer ::= '=' expr
get-set ::= get set?
get-set ::= set get
get ::= 'get' stmt-brace
set ::= 'set' set-name? stmt-brace
set-name ::= '(' identifier ')'
'var' declarations form the backbone of value declarations in Swift. A var declaration takes a pattern and an optional initializer, and declares all the pattern-identifiers in the pattern as variables. If there is an initializer and the pattern is fully-typed, the initializer is converted to the type of the pattern. If there is an initializer and the pattern is not fully-typed, the type of initializer is computed independently of the pattern, and the type of the pattern is derived from the initializer. If no initializer is specified, the pattern must be fully-typed, and the values are default-initialized.
If there is more than one pattern in a 'var' declaration, they are each considered independently, as if there were multiple declarations. The initial attribute-list is shared between all the declared variables.
A var declaration may contain a getter and (optionally) a setter,
which will be used when reading or writing the variable, respectively.
Such a variable does not have any associated storage. A var
declaration with a getter or setter must have a type (call it
T). The getter function, whose body is provided as part
of the var-get clause, has type () -> T.
Similarly, the setter function, whose body is part of the
var-set clause (if provided), has type (T)
-> (). If the var-set clause contains a var-set-name
clause, the identifier of that clause is used as the name of the
parameter to the setter. Otherwise, the parameter name is "value".
FIXME: Should the type of a pattern which isn't fully typed affect the type-checking of the expression (i.e. should we compute a structured dependent type)?
Like all other declarations, var's can optionally have a list of attributes applied to them.
The type of a variable must be
materializable. A variable is
an lvalue unless it has a var-get clause but not
var-set clause.
Here are some examples of var declarations:
// Simple examples.
var a = 4
var b : Int
var c : Int = 42
// This decodes the tuple return value into independently named parts
// and both 'val' and 'err' are in scope after this line.
var (val, err) = foo()
// Variable getter/setter
var _x : Int = 0
var x_modify_count : Int = 0
var x : Int {
get { return _x }
set {
x_modify_count = x_modify_count + 1
_x = value
}
}
Note that both 'get' and 'set' are context-sensitive keywords,
which means that at both global and local scope, there is a syntactic
ambiguity between a variable with a var-get-set clause
and a variable followed by a stmt-brace. This ambiguity is resolved
in favor of a variable with a var-get-set clause if the
token following the opening '{' is either 'set' or 'get'.
decl-func ::= 'static'? 'func' attribute-list any-identifier func-signature stmt-brace?
'func' is a declaration for a function. The argument list and optional return value are specified by the type production of the function, and the body is either a brace expression or elided. Like all other declarations, functions are can have attributes.
If the type is not syntactically a function type (i.e., has no -> in it at top-level), then the return value is implicitly inferred to be "()". All of the argument and return value names are injected into the scope of the function body.
A function in an extension of some type (or in other places that are semantically equivalent to an extension) implicitly get a 'this' argument with these rules ... [todo]
'static' functions are only allowed in an extension of some type (or in other places that are semantically equivalent to an extension). They indicate that the function is actually defined on the metatype for the type, not on the type itself. Thus it does not implicitly get a first 'this' argument, and can be used with dot syntax on the metatype.
TODO: Func should be an immutable name binding, it should implicitly add an attribute immutable when it exists.
TODO: Incoming arguments should be readonly, result should be implicitly writeonly when we have these attributes.
func-signature ::= func-arguments func-signature-result?
func-arguments ::= pattern-tuple+
func-arguments ::= selector-tuple
selector-tuple ::= '(' pattern-tuple-element ')' (identifier '(' pattern-tuple-element ')')+
func-signature-result ::= '->' type
A function signature specifies one or more sets of parameter patterns, plus an optional result type.
When a result type is not written, it is implicitly the empty tuple type, ().
In the body of the function described by a particular signature, all the variables bound by all of the parameter patterns are in scope, and the function must return a value of the result type.
An outermost pattern in a function signature must be fully-typed and irrefutable. If a result type is given, it must also be fully-typed.
The type of a function with signature (P0)(P1)..(Pn) -> R is T0 -> T1 -> .. -> Tn -> R, where Ti is the bottom-up type of the pattern Pi. This is called "currying". The behavior of all the intermediate functions (those which do not return R) is to capture their arguments, plus any arguments from prior patterns, and returns a function which takes the next set of arguments. When the "uncurried" function is called (the one taking Tn and returning R), all of the arguments are then available and the function body is finally evaluated as normal.
A function declared with a selector-style signature func(a0:T0) name1(a1:T1) .. namen(an:Tn) -> R has the type (_:T0, name1:T1, .. namen:Tn) -> R, that is, the names of the fields in the argument tuple are the namen identifiers preceding each argument pattern. However, in the body of a function described by a signature, those arguments will be bound using the corresponding an patterns inside the arguments. This allows for Cocoa-style keyword function names such as doThing(x, withThing=y) to be defined without requiring that an awkward keyword name be the same as the variable name.
Here are some examples of func definitions:
// Implicitly returns (), aka Void
func a() {}
// Same as 'a'
func a1() -> Void {}
// Function pointers to a function expression.
var a2 = func ()->() {}
var a3 = func () {}
var a4 = func {}
// Really simple function
func c(arg : Int) -> Int { return arg+4 }
// Simple operators.
func [infix_left=190] + (lhs : Int, rhs : Int) -> Int
func [infix_left=160] == (lhs : Int, rhs : Int) -> Bool
// Curried function with multiple return values:
func d(a : Int) (b : Int) -> (res1 : Int, res2 : Int) {
return (a,b)
}
// A more realistic example on a trivial type.
struct bankaccount {
amount : Int
static func bankaccount() -> bankaccount {
// Custom 'constructor' logic goes here.
}
func deposit(arg : Int) {
amount = amount + arg
}
static func someMetaTypeMethod() {}
}
// Dot syntax on metatype.
bankaccount.someMetaTypeMethod()
// A function with selector-style signature.
oneof PersonOfInterest {ColonelMustard, MissScarlet}
oneof Room {Conservatory, Ballroom}
oneof Weapon {Candlestick, LeadPipe}
func accuseSuspect(suspect:PersonOfInterest)
inRoom(room:Room)
withWeapon(weapon:Weapon) {
println("It was " + suspect + " in the " + room + " with the " +
weapon);
}
// Calling a selector-style function.
accuseSuspect(.ColonelMustard, inRoom=.Ballroom, withWeapon=.LeadPipe)
decl-typealias ::= typealias-head '=' type
typealias-head ::= 'typealias' identifier inheritance?
'typealias' makes a named alias of a type, like a typedef in C. From that point on, the alias may be used in all situations the specified name is. If an inheritance clause is provided, it specifies protocols to which the aliased type shall conform.
Here are some examples of type aliases:
// location is an alias for a tuple of ints.
typealias location = (x : Int, y : Int)
// pair_fn is a function that takes two ints and returns a tuple.
typealias pair_fn = (Int) -> (Int) -> (first : Int, second : Int)
decl-oneof ::= 'oneof' attribute-list identifier inheritance? oneof-body
oneof-body ::= '{' (oneof-element (',' oneof-element)*)? decl* '}'
oneof-element ::= identifier
oneof-element ::= identifier ':' type-annotation
A oneof declaration provides direct access to oneof types with a typealias declaration specifying a name. Please see oneof types for more information about their capabilities.
A 'oneof' may include a list of decls after its member types, which is syntactic sugar for defining an extension of the type. The limitations of an oneof extensions apply here as well.
Here are some examples of oneof declarations:
// Declare discriminated union with oneof decl.
oneof SomeInts {
None,
One : Int,
Two : (Int, Int)
}
// Declares three "enums".
oneof DataSearchFlags {
None, Backward, Anchored
}
func f1(searchpolicy : DataSearchFlags) // DataSearchFlags is a valid type name
func test1() {
f1(DataSearchFlags.None) // Use of constructor with qualified identifier
f1(.None) // Use of constructor with context sensitive type inference
// "None" has no type argument, so the constructor's type is "DataSearchFlags".
var a : DataSearchFlags = .None
}
oneof SomeMoreInts {
None, // Doesn't conflict with previous "None".
One : Int,
Two : (Int, Int)
}
func f2(a : SomeMoreInts)
func test2() {
// Constructors for oneof element can be used in the obvious way.
f2(.None)
f2(.One(4))
f2(.Two(1, 2))
// Constructor for None has type "SomeMoreInts".
var a : SomeMoreInts = SomeMoreInts.None
// Constructor for One has type "(Int) -> SomeMoreInts".
var b : (Int) -> SomeMoreInts = SomeMoreInts.One
// Constructor for Two has type "(Int,Int) -> SomeMoreInts".
var c : (Int,Int) -> SomeMoreInts = SomeMoreInts.Two
}
decl-struct ::= 'struct' attribute-list identifier inheritance? '{' decl-struct-body '}'
decl-struct-body ::= decl*
A struct declares a simple value type that can contain data members and have methods.
The body of a 'struct' is a list of decls. Non-property 'var' decls declare members with storage in the struct. Other declarations act like they would in an extension of the struct type.
Here are a few simple examples:
struct S1 {
var a : Int, b : Int
}
struct S2 {
var a : Int
func f() -> Int { return b }
var b : Int
}
Here are some more realistic examples of structs:
struct Point { x : Int, y : Int }
struct Size { width : Int, height : Int }
struct Rect {
origin : Point,
size : Size
typealias CoordinateType = Int
func area() -> Int { return size.width*size.height }
}
func test4() {
var a : Point
var b = Point.Point(1, 2) // Silly but fine.
var c = Point(y = 1, x = 2) // Using metatype.
var x1 = Rect(a, Size(42, 123))
var x2 = Rect(size = Size(width = 42, height=123), origin = a)
var x1_area = x1.width*x1.height
var x1_area2 = x1.area()
}
decl-class ::= 'class' attribute-list identifier inheritance? '{' decl-class-body '}'
decl-class-body ::= decl*
A class declares a reference type referring to an object which can contain data members and have methods. Classes support single inheritance; a parent class should be listed as the first type in the inheritance list.
The body of a 'class' is a list of decls. Non-property 'var' decls declare members with storage in the class. Non-static 'var' and 'func' decls declare instance members; static 'var' and 'func' decls declare members of the class itself. Both class and instance members can be overridden by a derived class.
Type declarations inside a class act essentially the same way as type declarations outside a class.
FIXME: For the moment, see classes.rst for more details on the class system.
FIXME: Add a reference to the section on generics.
The only way to create a new instance of a class is with a new expression.
Here is a simple example:
class C1 {
var a : Int
var b : Int
}
decl-protocol ::= 'protocol' attribute-list identifier inheritance? '{' protocol-member* '}'
A protocol declaration describes an abstract interface implemented by another type. It consists of a set of declarations, which may be instance methods or properties. A type conforms to a protocol if it provides declarations that correspond to each of the declarations in a protocol.
Here are some examples of protocols:
protocol Document {
var title : String
}
protocol-member ::= decl-func
'func' members of a protocol define a value of function type that may be accessed with dot syntax on a value of the protocol's type. The function gets an implicit "this" argument of the protocol type and shall not be static.
protocol-member ::= decl-var
'var' members of a protocol define "property" values that may be accessed with dot syntax on a value of the protocol's type. The actual variables have no storage, and will instead by accessed by a getter and setter. Thus, the variables shall have neither an initializer nor a getter/setter clause.
protocol-member ::= subscript-head
'subscript' members of a protocol define subscripting operations that may be accessed with the subscript operator ('[]') applied to a value of the protocol's type.
protocol-member ::= typealias-head
'typealias' members of a protocol define associated types, which are types used within the description of a protocol (typically in the inputs and outputs of 'func' members) that vary from one conforming type to another. When an associated type has an inheritance clause, any type meant to satisfy the associated type requirement must conform to each of the protocols specified within that inheritance clause.
protocol Enumerable {
typename EnumeratorType : Enumerator
func getElements() -> EnumeratorType
}
decl-subscript ::= subscript-head '{' get-set '}'
subscript-head ::= 'subscript' attribute-list pattern-tuple '->' type
A subscript declaration provides support for subscripting an object of a particular type via a getter and (optional) setter. Therefore, subscript declarations can only appear within a type definition or extension.
The pattern-tuple of a subscript declaration provides the indices that will be used in the subscript expression, e.g., the i in a[i]. This pattern must be fully-typed. The type following the arrow provides the type of element being accessed, which must be materializable. Subscript declarations can be overloaded, so long as either the pattern-tuple or type differs from other declarations.
The get-set clause specifies the getter and setter used for subscripting. The getter is a function whose input is the type of the pattern-tuple and whose result is the element type. Similarly, the setter is a function whose result type is () and whose input is the type of the pattern-tuple with a parameter of the element type added to the end of the tuple; the name of the parameter is the set-name, if provided, or value otherwise.
// Simple bit vector with storage for 64 boolean values
struct BitVector64 {
bits : Int64
// Allow subscripting with integer subscripts and a boolean result.
subscript (bit : Int) -> Bool {
// Getter tests the given bit
get {
if (bits & (1 << bit)) != 0 {
return true
}
return false;
}
// Setter sets the given bit to the provided value
set {
var mask = 1 << bit
if value {
bits = bits | mask
} else {
bits = bits & ~mask
}
}
}
}
var vec : BitVector64
vec[2] = true
if vec[3] {
print("third bit is set\n");
}
decl-constructor ::= 'constructor' attribute-list pattern-tuple stmt-brace
'constructor' declares a constructor for a class, struct, or oneof. Such a declaration is used whenever an object is constructed. Specifically, for classes, it is used when a new expression is written, and for structs and oneofs, it is used for function application when the "function" is a metatype.
FIXME: We haven't decided the precise rules for when constructors are implicitly declared. Default construction doesn't work right for structs or oneofs. We haven't decided what the restrictions are if a member isn't default-constructible.
A simple example:
struct X {
var member : Int
constructor(x : Int) {
member = x
}
}
var a = X(10)
decl-constructor ::= 'destructor' attribute-list stmt-brace
'destructor' declares a destructor for a class. This function is called when there are no longer any references to a class object, just before it is destroyed. Note that destructors can only be declared for classes, and cannot be declared in extensions.
FIXME: We haven't really decided the precise rules here, but it's probably a fatal error to either throw an exception or stash a reference to 'this' in a destructor. Not sure what happens when we cause the reference count of another object to reach zero inside a destructor. We might eventually allow destructors in extensions once we have ivars in extensions.
A simple example:
class X {
var fd : Int
destructor {
close(fd)
}
}
attribute-list ::= /*empty*/
attribute-list ::= lsquare-any ']'
attribute-list ::= lsquare-any attribute (',' attribute)* ']'
attribute ::= attribute-infix
attribute ::= attribute-resilience
attribute ::= attribute-byref
attribute ::= attribute-auto_closure
An attribute is a (possibly empty) comma separated list of attributes.
attribute-infix ::= 'infix_left' '=' integer_literal
attribute-infix ::= 'infix_right' '=' integer_literal
attribute-infix ::= 'infix '=' integer_literal
The infix attributes may only be applied to the declaration of a function of binary operator type whose name is an operator. The name indicates the associativity of the operator, either left associative, right associative, or non-associative.
FIXME: Implement these restrictions.
attribute-resilience ::= 'resilient'
attribute-resilience ::= 'fragile'
attribute-resilience ::= 'born_fragile'
See the resilience design.
attribute-byref ::= 'byref'
byref is only valid in a type-annotation that appears within either a pattern of a function-signature or the input type of a function type.
byref indicates that the argument will be passed "by reference": the bound variable will be an l-value.
The type being annotated must be materializable. The type after annotation is never materializable.
FIXME: we probably need a const-like variant, which permits
r-values (and avoids writeback when the l-value is not physical).
We may also need some way of representing this will be
consumed by the nth curry
.
attribute-auto_closure ::= 'auto_closure'
The auto_closure attribute modifies a function type, changing the behavior of any assignment into (or initialization of) a value with the function type. Instead of requiring that the rvalue and lvalue have the same function type, an "auto closing" function type requires its initializer expression to have the same type as the function's result type, and it implicitly binds a closure over this expression. This is typically useful for function arguments that want to capture computation that can be run lazily.
auto_closure is only valid in a type-annotation of a syntactic function type that is defined to take a syntactic empty tuple.
// An auto closure value. This captures an implicit closure over the
// specified expression, instead of the expression itself.
var a : [auto_closure] () -> Int = 4
// Definition of an 'assert' function. Assertions and logging routines
// often want to conditionally evaluate their argument.
func assert(condition : [auto_closure] () -> Bool)
// Definition of the || operator - it captures its right hand side as
// an autoclosure so it can short-circuit evaluate it.
func [infix_left=110] || (lhs: Bool, rhs: [auto_closure] ()->Bool) -> Bool
// Example uses of these functions:
assert(i < j)
if (a == 0 || b == 42) { ... }
type ::= type-simple
type ::= type-function
type ::= type-array
type-simple ::= type-identifier
type-simple ::= type-tuple
type-simple ::= type-composition
type-simple ::= type-metatype
type-annotation ::= attribute-list type
Swift has a small collection of core datatypes that are built into the compiler. Most datatypes that the user is exposed are defined by the standard library or declared as a user defined types.
FIXME: Why is array a type instead of type-simple?
Each type has a corresponding metatype, with the same name as the type, that is injected into the standard name lookup scope when a type is declared. This allows access to 'static functions' through dot syntax. For example:
// Declares a type 'foo' as well as its metatype.
struct foo {
static func bar() {}
}
// Declares x to be of type foo. A reference to a name in type context
// refers to the type itself.
var x : foo
// Accesses a static function on the foo metatype. In a value context, the
// name of its type refers to its metatype.
foo.bar()
A type may be fully-typed. A type is fully-typed unless one of the following conditions hold:
A type being 'fully-typed' informally means that the type is specified directly from its type annotation without needing contextual or other information to resolve its type.
A type may be materializable. A type is materializable unless it is 1) annotated with a byref attribute or 2) a tuple with a non-materializable element type. In general, variables must have materializable type.
type-identifier ::= identifier ('.' identifier)*
Named types may be used simply by using their name. Named types are introduced by typealias declarations or through type declarations that expand to one.
typealias location = (x : Int, y : Int)
var x : location // use of a named type.
Type names may use dot syntax to refer to names types declared in other modules or types nested within other types.
// Direct reference to a member of another module.
var x : swift.Int
type-tuple ::= lparen-any type-tuple-body? ')'
type-tuple-body ::= type-tuple-element (',' type-tuple-element)* '...'?
type-tuple-element ::= identifier value-specifier
type-tuple-element ::= type-annotation
Syntactically, tuple types are simply a (possibly empty) list of elements enclosed in parentheses. A tuple type with a single, anonymous, undefaulted element is exactly that type: the parentheses are treated as grouping parentheses.
Tuples are the low-level form of data aggregation in Swift, and are used as the building block of function argument lists, multiple return values, oneof bodies, etc. Because tuples are widely accessible and available everywhere in the language, aggregate data access and transformation is uniform and powerful.
Each element of a tuple contains an optional name followed by a type and/or a default value expression, whose type conversion rules work like those in a var declaration. The name affects swizzling of elements in the tuple when tuple conversions are performed.
For tuples with elements with default values, the default value is evaluated when converting an expression to that tuple type if it is used, or when a value of that tuple type is default-initialized. The default value is not allowed to refer to local declarations. FIXME: Maybe we should relax this?
If the tuple body ends with '...', the tuple is a varargs tuple. The type of the last element is changed from T to T[], and there are special rules for converting an expression to varargs tuple type.
// Variable definitions.
var a : ()
var b : (Int, Int)
var c : (x : (), y : Int)
var d : (a : Int, b = 4) // Value is initialized to (0,4)
var e : (a : Int, b = 4) = (1) // Value is initialized to (1,4)
// Tuple type inferred from an initializers:
var m = () // Type = ()
var n = (x = 1, y = 2) // Type = (x : Int, y : Int)
var o = (1, 2, 3) // Type = (Int, Int, Int)
// Function argument and result is a tuple type.
func foo(x : Int, y : Int) -> (val : Int, err : Int)
// oneof and struct declarations with tuple values.
struct S { a : Int, b : Int }
oneof Vertex {
Point2 : (x : Int, y : Int),
Point3 : (x : Int, y : Int, z : Int),
Point4 : (w : Int, x : Int, y : Int, z : Int)
}
type-function ::= type-tuple '->' type
Function types have a single input and single result type, separated by an arrow. Because each of the types is allowed to be a tuple, we trivially support multiple arguments and multiple results. "Function" types are more properly known as a "closure" type, because they can embody any context captured when the function value was formed.
The result type of a function type must be materializable. The argument type of a function is always required to be parenthesized (a tuple). The behavior of function types may be modified with the auto_closure attribute.
Because of the grammar structure, a nested function type like "(a) -> (b) -> c" is parsed as "(a) -> ((b) -> c)". This means that if you declare this that you can pass it one argument to get a function that "takes b and returns c" or you can pass two arguments to "get a c". This is known as currying. For example:
// A simple function that takes a tuple and returns Int:
var a : (a : Int, b : Int) -> Int
// A simple function that returns multiple values:
var a : (a : Int, b : Int) -> (val: Int, err: Int)
// Declare a function that returns a function:
var x : (Int) -> (Int) -> Int
// y has type (Int) -> Int
var y = x(1)
// z1 and z2 both has type Int, and both have the same value (assuming
// the function had no side effects).
var z1 = x(1)(2)
var z2 = y(2)
// An auto closure value. This captures an implicit closure over the
// specified expression, instead of the expression itself.
var a : [auto_closure] () -> Int = 4
A oneof type is a simple discriminated union: the runtime representation of a value of oneof type only has one of the specified elements at a time.
All of the element types of a oneof type must be materializable.
A oneof type is defined by a oneof decl.
A default initialized value of oneof type is initialized to the first element type in the list, with the default value for its element type.
The oneof metatype has a member corresponding to each declared element. For elements with a declared type, this member is a function which can construct a oneof containing that element. For elements without a declared type, the member is simply a oneof value for that element. A oneof value has no accessible members except those explicitly defined by the user.
A reference to a member of the oneof metatype can be shortened using delayed identifier resolution with context sensitive type inference.
TODO: Should attributes be allowed on oneof elements? TODO: Eventually, with generics we'll have equality and inequality operators. Oneof decls should implicitly define these for their types. TODO: Need pattern matching and element extraction.
type-array ::= type-simple
type-array ::= type-array lsquare-unspaced ']'
type-array ::= type-array lsquare-unspaced expr ']'
Array types include a base type and an optional size. Array types indicate a linear sequence of elements stored consequtively memory. Array elements may be efficiently indexed in constant time. All array indexes are bounds checked and out of bound accesses are diagnosed with either a compile time or runtime failure (TODO: runtime failure mode not specified).
While they look syntactically very similar, an array type with a size has very different semantics than an array without. In the former case, the type indicates a declaration of actual storage space. In the later case, the type indicates a reference to storage space allocated elsewhere of runtime-specified size.
FIXME: We should separate out "Arrays" from "Slices". Arrays should always require a size and is by-value, a slice is a by-ref and never have a (statically specified) size.
For an array with a size, the size must be more than zero (no indices would be valid). For now, the array size must be a literal integer. TODO: Define a notion like C's integer-constant-expression for how constant folding works.
The element type of an array type must be materializable.
FIXME: Int[][] not valid because the element type isn't sized. We need some constraint to reject this, or do we?
Some example array types:
// A simple array declaration:
var a : Int[4]
// A reference to another array:
var b : Int[] = a
// Declare a two dimensional array:
var c : Int[4][4]
// Declare a reference to another array, two dimensional:
var d : Int[4][]
// Declare an array of function pointers:
var array_fn_ptrs : (: (Int) -> Int)[42]
var g = array_fn_ptrs[12](4)
// Without parens, this is a function that returns a fixed size array:
var fn_returning_array : (Int) -> Int[42]
var h : Int[42] = fn_returning_array(4)
// You can even have arrays of tuples and other things, these work right
// through composition:
var array_of_tuples : (a : Int, b : Int)[42]
var tuple_of_arrays : (a : Int[42], b : Int[42])
array_of_tuples[12].a = array_of_tuples[13].b
tuple_of_arrays.a[12] = array_of_tuples.b[13]
type-metatype ::= type-simple '.' 'metatype'
Every type has an associated metatype. A value of the metatype type is a reference to a global object which describes the type. Most metatype types are singleton and therefore require no storage, but metatypes associated with class types follow the same subtyping rules as their associated class types and therefore are not singleton.
type-composition ::= 'protocol' '<' type-composition-list? '>'
type-composition-list ::= type-identifier (',' type-identifier)*
A protocol composition type composes together a number of
protocols to describe a type that meets the requirements of each of
those protocols. A protocol composition type protocol<A,
B> is similar to an explicitly-defined protocol that
inherits both A and B
protocol C : A, B { }
but without the need to introduce a new name.
Each of the types named in the
type-composition-list shall refer to either a protocol
or to a protocol composition. The list may be empty, in which case
every type conforms to the empty protocol composition. This is how
the Any type is defined in the standard library.
// A value that represents any type
var any : protocol<> = 17
// A value that conforms to both the Document and Enumerator protocols
var doc : protocol<Document,Enumerator>
doc.isEmpty() // uses Enumerator.isEmpty()
doc.title = "Hello" // uses Document.title
inheritance ::= ':' type-identifier (',' type-identifier)*
A named type (e.g., a class, struct, oneof, or protocol) can "inherit" some set of protocols, which implies that any object of that type conforms to each of those protocols. When a protocol inherits other protocols, the set of requirements from all of those protocols is effectivel aggregated into the protocol, and a type that conforms to the current protocol shall conform to each of the protocols that it inherits.
When a non-protocol type inherits a protocol, it is specifying explicitly that it conforms to that protocol. The program is ill-formed if the type does not conform to the protocol.
protocol VersionedDocument : Document { // every VersionedDocument is a Document
func bumpVersion()
}
func print(doc : Document) { /* ... */ }
var myDocument : VersionedDocument;
print(myDocument) // okay: a VersionedDocument is a Document
class StoredHTML : VersionedDocument { // okay: StoredHTML conforms to VersionedDocument
var Title : String
func bumpVersion()
}
pattern-atom ::= pattern-identifier
pattern-atom ::= pattern-tuple
pattern ::= pattern-atom
pattern ::= pattern-typed
The basic pattern grammar is a literal "atom" followed by an optional type annotation. Type annotations are useful for documentation, as well as for coercing a matched expression to a particular kind. They are also required when patterns are used in a function signature.
A pattern has a type. A pattern may be "fully-typed", meaning informally that its type is fully determined by the type annotations it contains. Some patterns may also derive a type from their context, be it an enclosing pattern or the way it is used; this set of situations is not yet fully determined.
A pattern may be "irrefutable", meaning informally that it matches all values of its type. Patterns in some contexts are required to be irrefutable.
pattern-typed ::= pattern-atom ':' type-annotation
A type annotation constrains a pattern to have a specific type. An annotated pattern is fully-typed if its annotation type is fully-typed. It is irrefutable if and only if its subpattern is irrefutable.
pattern-identifier ::= identifier
An identifier pattern binds a value to a particular name, which is then a legal variable of the pattern's type within its scope. It is irrefutable. It is not fully-typed; the type must be inferred from context.
As a special case, if the identifier is _ then no variable comes into scope, and the value matched is lost. Such an identifier pattern is called an "ignore pattern". An identifier pattern which is not an ignore pattern is called a "named pattern".
The type of a named pattern must be materializable unless it appears in a function-signature and is directly a byref-annotated type.
pattern-tuple ::= '(' pattern-tuple-body? ')'
pattern-tuple-body ::= pattern-tuple-element (',' pattern-tuple-body)* '...'?
pattern-tuple-element ::= pattern
pattern-tuple-element ::= pattern '=' expr
A tuple pattern is a list of zero or more patterns. Within a function signature, patterns may also be given a default-value expression.
A tuple pattern is irrefutable if all its sub-patterns are irrefutable.
A tuple pattern is fully-typed if all its sub-patterns are fully-typed, in which case its type is the corresponding tuple type, where each type-tuple-element has the type, label, and default value of the corresponding pattern-tuple-element. A pattern-tuple-element has a label if it is a named pattern or a type annotation of a named pattern.
A tuple pattern whose body ends in '...' is a varargs tuple. The last element of such a tuple must be a typed pattern, and the type of that pattern is changed from T to T[]. The corresponding tuple type for a varargs tuple is a varargs tuple type.
As a special case, a tuple pattern with one element that has no label, has no default value, and is not varargs is treated as a grouping parenthesis: it has the type of its constituent pattern, not a tuple type.
expr ::= expr-unary expr-binary*
expr-primary ::= expr-literal
expr-primary ::= expr-identifier
expr-primary ::= expr-explicit-closure
expr-primary ::= expr-anon-closure-arg
expr-primary ::= expr-paren
expr-primary ::= expr-delayed-identifier
expr-primary ::= expr-func
expr-postfix ::= expr-primary
expr-postfix ::= expr-postfix operator-postfix
expr-postfix ::= expr-new
expr-postfix ::= expr-dot
expr-postfix ::= expr-metatype
expr-postfix ::= expr-subscript
expr-postfix ::= expr-call
At the top level of the expression grammar, expressions are a sequence of unary expressions joined by binary operators. When parsing an expr, a binary operator immediately following an expr-unary continues the expression, and the program is ill-formed if it is not then followed by another expr-unary. This resolves an ambiguity which could otherwise arise in statement contexts due to semicolon elision.
5 !- +~123 -+- ~+6
(foo)(())
bar(49+1)
baz()
expr-binary ::= operator-binary expr-unary
Infix binary expressions are not formed during parsing. Instead, they are formed after name resolution by building a tree from an operator-delimited sequence of unary expressions. Precedence and associativity are determined by the infix attribute on the resolved names, which must fully agree.
If an operator is used as a binary operator, but name resolution does not find at least one function of binary operator type, the expression is ill-formed.
A simple example is:
4 + 5 * 123
expr-unary ::= operator-prefix* expr-postfix
If an operator is used as a unary operator, but name resolution does not find at least one function that takes a single argument, the expression is ill-formed.
Simple examples:
i = -j
expr-literal ::= integer_literal
expr-literal ::= floating_literal
expr-literal ::= character_literal
expr-literal ::= string_literal
Numeric literals are either integer, floating point, character, or string depending on its lexical form. The type of the literal is inferred based on its context. If there is no contextual type information for an expression, all unresolved types are inferred to 'IntegerLiteralType' type, to 'FloatLiteralType', to 'CharacterLiteralType', and to 'StringLiteralType', respectively. If a literal is used and these types are not defined, then the code is malformed.
A literal is compatible with its inferred type if that type implements an informal protocol required by literals. This informal protocol requires that the type have an unambiguous "static" function defined whose result type is the same as the inferred type, and that takes a single argument that is either itself literal compatible, or is a builtin integer type.
expr-identifier ::= identifier
A raw identifier refers to a value found via unqualified value lookup, and has the type of the declaration returned by name lookup and overload resolution. Value declarations are installed with var and the syntactic sugar forms like func declarations.
expr-explicit-closure ::= '{' expr? '}'
A closure expression is a super-concise version of expr-func for cases where very simple predicates and other small closures are needed (e.g. sorting and searching predicates). It uses Swift's aggressive type system to infer both the argument and return values types for the closure from the context it is used in, and allows access to the formal arguments of the closure through anonymous closure argument expressions. In tuples with no body expression, the '()' expression is used as the result.
It is illegal to use these expressions when there is insufficient context to infer the argument and return types of the closure.
Note that expr-explicit-closure is ambiguous with stmt-brace when used in a another stmt-brace or in translation-unit scope. This ambiguity is resolved towards stmt-brace, because these context never have enough contextual information to infer the type of the closure, thus they would always be a semantic error if parsed that way.
// Takes a closure that it calls to determine an ordering relation.
func magic(val : Int, predicate : (a : Int, b : Int) -> Bool)
func f() {
// Compare one way. Closure is inferred to return Bool and take two ints
// from the argument context. This same information infers that $0 and $1
// both have type 'Int'.
magic(42, { $0 < $1 })
// Compare the other way way.
magic(42, { $1 < $0 })
// Error, not enough context to infer the type of $0.
var x = { $0 }
}
expr-anon-closure-arg ::= dollarident
A use of an identifier whose name fits the "$[0-9]+" regular expression is a reference to an anonymous closure argument that is formed when the containing expression is coerced into a closure context. All other dollar identifiers are invalid.
This can only be used in the body of an expr-explicit-closure.
expr-delayed-identifier ::= '.' identifier
A delayed identifier expression refers to a constructor of a oneof type, without knowing which type it is referring to. The expression is resolved to a constructor of a concrete type through context sensitive type inference.
oneof Direction { Up, Down }
func search(val : Int, direction : Direction)
func f() {
search(42, .Up)
search(17, .Down)
}
expr-paren ::= lparen-any ')'
expr-paren ::= lparen-any expr-paren-element (',' expr-paren-element)* ')'
expr-paren-element ::= (identifier '=')? expr
Parentheses expressions contain an (optionally empty) list of optionally named values. Parentheses in an expression context denote one of two things: 1) grouping parentheses, or 2) a tuple literal.
Grouping parentheses occur when there is exactly one value in the list and that value does not have a name. In this case, the type of the parenthesis expression is the type of the single value.
All other cases are tuple literals. The type of the expression is a tuple type whose elements and order match that of the initializer. If there are any named elements, those elements become names for the tuple type. A parenthesis expression with no value has a type of the empty tuple.
Note that some enclosing productions restrict the lparen-any to a lparen-unspaced.
Some examples:
// Simple grouping parenthesis.
var a = (4) // Type = Int
var b = (4+a) // Type = Int
// Tuple literals.
var c = () // Type = ()
var d = (4, 5) // Type = (:Int,:Int)
var e = (c, d) // Type = ((), (:Int, :Int))
var f = (x = 4, y = 5) // Type = (x : Int, y : Int)
var g = (4, y = 5, 6) // Type = (:Int, y : Int, :Int)
// Named arguments to functions.
func foo(a : Int, b : Int)
foo(b = 4, a = 1)
expr-func ::= 'func' func-signature? stmt-brace
A func expression is an anonymous (unnamed) function literal definition, which can define named arguments (and whose names are in scope for its body) and that can refer to values defined in parent scopes.
A func expression captures a reference to any values in parent scopes that are used.
If the function signature is omitted, it is implicitly () -> ().
TODO: Allow attributes on funcs when useful.
// A simple func expression.
var a = func(val : Int) { print(val+1) }
// A recursive func expression.
var fib = func(n : Int) -> Int {
if (n < 2) { return n; }
return fib(n-1)+fib(n-2)
}
expr-dot ::= expr-postfix '.' dollarident
If the base expression has tuple type, then the magic identifier "$[0-9]+" accesses the specified anonymous member of the tuple. Otherwise, this form is invalid.
expr-dot ::= expr-postfix '.' identifier
If the base expression has tuple type and if the identifier is the name of a field in the tuple, then this is a reference to the specified field.
Otherwise, dot name lookup is performed, and this expression is treated as function application. This allows looking up members in modules, metatypes, etc.
expr-metatype ::= expr-postfix '.' 'metatype'
A metatype expression produces the metatype for the dynamic type of the value of the base expression. The base expression is converted to an rvalue and evaluated, and then the result is calculated as follows:
expr-subscript ::= expr-postfix lsquare-unspaced expr ']'
A subscript expression invokes a subscript getter or setter on the type of the expr-postfix. The expr is used as the subscript argument, which will be provided to either the getter or setter depending on whether the subscript expression is used as an rvalue (reading) or lvalue (writing), respectively. A subscript expression that resolves to a subscript declaration with no setter cannot be modified.
expr-new ::= 'new' type-identifier expr-new-bounds?
expr-new-bounds ::= expr-new-bound
expr-new-bounds ::= expr-new-bounds expr-new-bound
expr-new-bound ::= lsquare-unspaced expr? ']'
Allocates and initializes a new array of objects with value semantics or an individual object with reference semantics. If any bounds clauses are present, the first clause must have an expression; subsequent bounds, if present, must be constant under the usual rules for array types.
expr-call ::= expr-postfix expr-paren
The leading '(' of the expr-paren must be a lparen-unspaced. This greatly reduces the likelihood of confusion from semicolon elision, without requiring feedback from the typechecker or more aggressive whitespace sensitivity.
If the expr-prefix refers to a (possibly parenthesized) name of a type, the expr-paren is first coerced to the type named by expr-prefix. If that coercion fails, then the expr-prefix refers to the set of constructors for that type, which consists of:
Simple examples:
// Application of an empty tuple to the function f.
f()
// Application of 4 to the function f.
g(4)
// Application of 4 to the function returned by h().
var h : (Int) -> (Int) -> Int
...
h()(4)
// Two separate statements
i()
(j <+ 2)()
stmt ::= stmt-semicolon
stmt ::= stmt-assign
stmt ::= stmt-brace
stmt ::= stmt-return
stmt ::= stmt-if
stmt ::= stmt-while
stmt ::= stmt-for-c-style
stmt ::= stmt-for-each
Statements provide the control flow constructs of function bodies and top-level code.
// A function with some statements.
func fib(v : Int) -> Int {
if v < 2 {
return v
}
return fib(v-1)+fib(v-2)
}
stmt-semicolon ::= ';'
The semicolon statement has no effect.
if (x = 1)It also implies that nested assignments are also illegal:
x = y = z
stmt-assign ::= expr '=' expr
The assignment statement evaluates its left hand side as some sort of lvalue, then evaluates the right hand side, the assigns one to the other. FIXME: The requirements for lvalues should be described, and tied into a description of lvalue types.
stmt-brace ::= '{' stmt-brace-item* '}'
stmt-brace-item ::= decl
stmt-brace-item ::= expr
stmt-brace-item ::= stmt
The brace statement provides a sequencing operation which evaluates the members of its body in order. Function bodies and the bodies of control flow statements use braces. Also, the translation unit itself is effectively and brace statement without the braces.
stmt-return ::= 'return' expr
stmt-return ::= 'return'
The return statement sets the return value of the current func declaration or func expression and transfers control out of the function. It sets the return value by converting the specified expression result (or '()' if none is specified) to the return type of the 'func'.
The stmt-return grammar is ambiguous: "{ return 4 }" could be parsed as {"return" "4"} or as a single statement. Ambiguity here is resolved toward the first production, because control flow can't transfer to an subexpression.
stmt-if ::= 'if' expr stmt-brace stmt-if-else?
stmt-if-else ::= 'else' stmt-brace
stmt-if-else ::= 'else' stmt-if
'if' statements provide a simple control transfer operations that evaluates the condition, invokes the 'getLogicValue' member of the result if the result not a 'Bool', then determines the direction of the branch based on the result. (Internally, the standard library type 'Bool' has a getLogicValue member that returns a 'Builtin.Int1'.) It is an error if the type of the expression is context-dependent or some non-Bool type.
Some examples include:
if true {
/*...*/
}
if X == 4 {
} else {
}
if X == 4 {
} else if X == 5 {
} else {
}
stmt-while ::= 'while' expr stmt-brace
'while' statements provide simple loop construct which (on each iteration of the loop) evalutes the condition, invokes the 'getLogicValue' member of the result if the result not a 'Bool', then determines whether to keep looping. (Internally, the standard library type 'Bool' has a getLogicValue member that returns a 'Builtin.Int1'.) It is an error if the type of the expression is context-dependent or some non-Bool type.
Some examples include:
while true {
/*...*/
}
while X == 4 {
X = 3
}
stmt-do-while ::= 'do' stmt-brace 'while' 'expr
'do-while' statements provide simple loop construct which (on each iteration of the loop) evaluates the body, then evaluates the condition, invoking the 'getLogicValue' member of the result if the result not a 'Bool', then determines whether to keep looping. (Internally, the standard library type 'Bool' has a getLogicValue member that returns a 'Builtin.Int1'). It is an error if the type of the expression is context-dependent or some non-Bool type.
Some examples include:
do {
/*...*/
} while true
do {
X = 3
} while X == 4
stmt-for-c-style ::= 'for' stmt-for-c-style-init? ';' expr? ';' expr-or-stmt-assign? stmt-brace
expr-or-stmt-assign ::= expr | stmt-assign
stmt-for-c-style-init ::= decl-var
stmt-for-c-style-init ::= expr-or-stmt-assign
C-Style 'for' statements provide simple loop construct which evaluates the first part (the initializer) before entering the loop, then evalutes the second condition as a logic value to determines whether to keep looping. The third condition is executed at the end of the loop. All three are evaluated in a new scope that surrounds the for statement.
Some examples include:
for i = 0; i != 10; ++i {
/*...*/
}
for var (i,j) = (0,1); i != 10; ++i {
/*...*/
}
stmt-for-each ::= 'for' pattern 'in' expr stmt-brace
Enumerator-based 'for' statements provide iteration over the values in a container. The expr is the container, and shall have a method getElements() that, when called, returns the range over which the loop will iterate.
The range must, at a minimum, have two methods:
Note that each iteration of the loop declares a distinct variable for each variable in the pattern. For example, in a loop like "for i in 0..10", if i is captured inside the loop, each iteration captures a different "i", so there would be a total of ten versions generated each time the loop runs.
Some examples include:
for i in 0..100 {
println(String(i));
}
Name binding in swift is performed in different ways depending on what language entity is being considered:
Value names (for var and func declarations) and type names (for typealias, oneof, and struct declarations) follow the same scope and name lookup rules as described below.
tuple element names
scope within oneof decls
Context sensitive member references are resolved during type checking.
Basic algo:
Shadowing: Given a ValueDecl D1 in the current module and a ValueDecl D2 in an imported module with the same name and a member of the same type (if relevant): 1. If D1 is a TypeDecl, D2 is shadowed. 2. If neither D1 nor D2 is a TypeDecl, and they have the same type, D2 is shadowed. If a declaration in an imported module is shadowed by any declaration in the current module, it is not found by unqualified global lookup or lookup for members of a type.
Dot Expressions bind to name of tuple elements.
Binary expressions, function application, etc.
This describes some of the standard swift code as it is being built up. Since Swift is designed to give power to the library developers, much of what is normally considered the "language" is actually just implemented in the library.
All of this code is published by the 'swift' module, which is implicitly imported into each translation unit, unless some sort of pragma in the code (attribute on an import?) is used to change or disable this behavior.
In the initial Swift implementation, a module named Builtin is imported into every file. Its declarations can only be found by dot syntax. It provides access to a small number of primitive representation types and operations defined over them that map directly to LLVM IR.
The existance of and details of this module are a private implementation detail used by our implementation of the standard library. Swift code outside the standard library should not be aware of this library, and an independent implementation of the swift standard library should be allowed to be implemented without the builtin library if it desires.
For reference below, the description of the standard library uses the "Builtin." namespace to refer to this module, but independent implementations could use another implementation if they so desire.
// Void is just a type alias for the empty tuple.
typealias Void = ()
// Fixed size types are simple structs of the right size.
struct Int8 { value : Builtin.Int8 }
struct Int16 { value : Builtin.Int16 }
struct Int32 { value : Builtin.Int32 }
struct Int64 { value : Builtin.Int64 }
struct Int128 { value : Builtin.Int128 }
// Int is just an alias for the 64-bit integer type.
typealias Int = Int64
struct Float { value : Builtin.FPIEEE32 }
struct Double { value : Builtin.FPIEEE64 }
// Bool is a simple discriminated union.
oneof Bool {
true, false
}
// Allow true and false to be used unqualified.
var true = Bool.true
var false = Bool.false
// Simple binary operators, following the same precedence as C.
func [infix_left=200] * (lhs: Int, rhs: Int) -> Int
func [infix_left=200] / (lhs: Int, rhs: Int) -> Int
func [infix_left=200] % (lhs: Int, rhs: Int) -> Int
func [infix_left=190] + (lhs: Int, rhs: Int) -> Int
func [infix_left=190] - (lhs: Int, rhs: Int) -> Int
// In C, <<, >> is 180.
func [infix_left=170] < : (lhs : Int, rhs : Int) -> Bool
func [infix_left=170] > : (lhs : Int, rhs : Int) -> Bool
func [infix_left=170] <= : (lhs : Int, rhs : Int) -> Bool
func [infix_left=170] >= : (lhs : Int, rhs : Int) -> Bool
func [infix_left=160] == : (lhs : Int, rhs : Int) -> Bool
func [infix_left=160] != : (lhs : Int, rhs : Int) -> Bool
// In C, bitwise logical operators are 130,140,150.
func [infix_left=120] && (lhs: Bool, rhs: ()->Bool) -> Bool
func [infix_left=110] || (lhs: Bool, rhs: ()->Bool) -> Bool
// In C, 100 is ?:
// In C, 90 is =, *=, += etc.