================================================================================
Type references
================================================================================

something as Int
something as? A
something as! A

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier)))
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier)))
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier))))

================================================================================
Optional casts followed by nil-coalescing
================================================================================

let x = y as? String ?? "z"
let flag = d.get() as? Bool ?? true

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (nil_coalescing_expression
      (as_expression
        (simple_identifier)
        (as_operator)
        (user_type
          (type_identifier)))
      (line_string_literal
        (line_str_text))))
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (nil_coalescing_expression
      (as_expression
        (call_expression
          (navigation_expression
            (simple_identifier)
            (navigation_suffix
              (simple_identifier)))
          (call_suffix
            (value_arguments)))
        (as_operator)
        (user_type
          (type_identifier)))
      (boolean_literal))))

================================================================================
Nested types
================================================================================

something as Some.NestedType

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier)
      (type_identifier))))

================================================================================
Deeply nested types
================================================================================

somethingElse as A.Deeply.Nested.Type

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier)
      (type_identifier)
      (type_identifier)
      (type_identifier))))

================================================================================
Generic parameterized types
================================================================================

something as Generic<T>
something as Generic<A, Type>

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier)
      (type_arguments
        (user_type
          (type_identifier)))))
  (as_expression
    (simple_identifier)
    (as_operator)
    (user_type
      (type_identifier)
      (type_arguments
        (user_type
          (type_identifier))
        (user_type
          (type_identifier))))))

================================================================================
Function types
================================================================================

unitFunction as () -> Unit
consumer as (Int) -> Unit
configurator as (inout Config) -> Unit

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (function_type
      (tuple_type)
      (user_type
        (type_identifier))))
  (as_expression
    (simple_identifier)
    (as_operator)
    (function_type
      (tuple_type
        (tuple_type_item
          (user_type
            (type_identifier))))
      (user_type
        (type_identifier))))
  (as_expression
    (simple_identifier)
    (as_operator)
    (function_type
      (tuple_type
        (tuple_type_item
          (parameter_modifiers
            (parameter_modifier))
          (user_type
            (type_identifier))))
      (user_type
        (type_identifier)))))

================================================================================
Function types with multiple parameters
================================================================================

a as (Int, Generic<T>, Boolean) -> Unit
b as (Nested.Type, (Int)) -> Unit

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (function_type
      (tuple_type
        (tuple_type_item
          (user_type
            (type_identifier)))
        (tuple_type_item
          (user_type
            (type_identifier)
            (type_arguments
              (user_type
                (type_identifier)))))
        (tuple_type_item
          (user_type
            (type_identifier))))
      (user_type
        (type_identifier))))
  (as_expression
    (simple_identifier)
    (as_operator)
    (function_type
      (tuple_type
        (tuple_type_item
          (user_type
            (type_identifier)
            (type_identifier)))
        (tuple_type_item
          (tuple_type
            (tuple_type_item
              (user_type
                (type_identifier))))))
      (user_type
        (type_identifier)))))

================================================================================
Types with named parameters (function or tuple)
================================================================================

a as (first: A, second: B)
-> Unit
let c: (third: C, fourth: D)

--------------------------------------------------------------------------------

(source_file
  (as_expression
    (simple_identifier)
    (as_operator)
    (function_type
      (tuple_type
        (tuple_type_item
          (simple_identifier)
          (user_type
            (type_identifier)))
        (tuple_type_item
          (simple_identifier)
          (user_type
            (type_identifier))))
      (user_type
        (type_identifier))))
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (tuple_type
        (tuple_type_item
          (simple_identifier)
          (user_type
            (type_identifier)))
        (tuple_type_item
          (simple_identifier)
          (user_type
            (type_identifier)))))))

================================================================================
Nested optional types
================================================================================

private var dictionary: [String: Any?]?

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (modifiers
      (visibility_modifier))
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (optional_type
        (dictionary_type
          (user_type
            (type_identifier))
          (optional_type
            (user_type
              (type_identifier))))))))

================================================================================
Double optional in lambda parameter
================================================================================

let closure = { (v: AnyObject??) -> String? in
    return nil
}

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (lambda_literal
      (lambda_function_type
        (lambda_function_type_parameters
          (lambda_parameter
            (simple_identifier)
            (optional_type
              (user_type
                (type_identifier)))))
        (optional_type
          (user_type
            (type_identifier))))
      (statements
        (control_transfer_statement
          (nil_literal))))))

================================================================================
Double optional array element in lambda parameter (ReactKit regression)
================================================================================

let closure = { (values: [AnyObject??], _) -> String? in nil }

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (lambda_literal
      (lambda_function_type
        (lambda_function_type_parameters
          (lambda_parameter
            (simple_identifier)
            (array_type
              (optional_type
                (user_type
                  (type_identifier)))))
          (lambda_parameter
            (simple_identifier)))
        (optional_type
          (user_type
            (type_identifier))))
      (statements
        (nil_literal)))))

================================================================================
Implicitly unwrapped optional types
================================================================================

private var dictionary: [String: Any?]!

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (modifiers
      (visibility_modifier))
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (dictionary_type
        (user_type
          (type_identifier))
        (optional_type
          (user_type
            (type_identifier)))))))

================================================================================
Type aliases
================================================================================

public typealias Callback<T> = (T) -> Void
public typealias IntCallback = Callback<T>

--------------------------------------------------------------------------------

(source_file
  (typealias_declaration
    (modifiers
      (visibility_modifier))
    (type_identifier)
    (type_parameters
      (type_parameter
        (type_identifier)))
    (function_type
      (tuple_type
        (tuple_type_item
          (user_type
            (type_identifier))))
      (user_type
        (type_identifier))))
  (typealias_declaration
    (modifiers
      (visibility_modifier))
    (type_identifier)
    (user_type
      (type_identifier)
      (type_arguments
        (user_type
          (type_identifier))))))

================================================================================
Metatypes
================================================================================

_ = foo as [String].Type

protocol GetType {
   func getType() -> AnyObject.Type
}

--------------------------------------------------------------------------------

(source_file
  (assignment
    (directly_assignable_expression
      (simple_identifier))
    (navigation_expression
      (as_expression
        (simple_identifier)
        (as_operator)
        (array_type
          (user_type
            (type_identifier))))
      (navigation_suffix
        (simple_identifier))))
  (protocol_declaration
    (type_identifier)
    (protocol_body
      (protocol_function_declaration
        (simple_identifier)
        (user_type
          (type_identifier)
          (type_identifier))))))

================================================================================
Existential types
================================================================================

let p: any P = S()
func q(using p: any P) { }

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (existential_type
        (user_type
          (type_identifier))))
    (call_expression
      (simple_identifier)
      (call_suffix
        (value_arguments))))
  (function_declaration
    (simple_identifier)
    (parameter
      (simple_identifier)
      (simple_identifier)
      (existential_type
        (user_type
          (type_identifier))))
    (function_body)))

================================================================================
Typed throws in function type
================================================================================

let closure: () throws(MyError) -> Void = {}

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (function_type
        (tuple_type)
        (throws_clause
          (user_type
            (type_identifier)))
        (user_type
          (type_identifier))))
    (lambda_literal)))

================================================================================
Bracket-qualified nested type in generic argument
================================================================================

func f(range: Range<[String].Index>) {}

--------------------------------------------------------------------------------

(source_file
  (function_declaration
    (simple_identifier)
    (parameter
      (simple_identifier)
      (user_type
        (type_identifier)
        (type_arguments
          (bracket_qualified_type
            (array_type
              (user_type
                (type_identifier)))
            (type_identifier)))))
    (function_body)))

================================================================================
Integer generic parameters
================================================================================

struct Matrix<let rows: Int, let cols: Int> {}

--------------------------------------------------------------------------------

(source_file
  (class_declaration
    (type_identifier)
    (type_parameters
      (type_parameter
        (type_identifier)
        (user_type
          (type_identifier)))
      (type_parameter
        (type_identifier)
        (user_type
          (type_identifier))))
    (class_body)))

================================================================================
Inline array type sugar
================================================================================

let a: [3 of Int] = [1, 2, 3]

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (array_type
        (integer_literal)
        (user_type
          (type_identifier))))
    (array_literal
      (integer_literal)
      (integer_literal)
      (integer_literal))))

================================================================================
Integer type argument
================================================================================

var b = InlineArray<4, Int>(repeating: 0)

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (constructor_expression
      (user_type
        (type_identifier)
        (type_arguments
          (integer_literal)
          (user_type
            (type_identifier))))
      (constructor_suffix
        (value_arguments
          (value_argument
            (value_argument_label
              (simple_identifier))
            (integer_literal)))))))

================================================================================
Optional member of a bracket-qualified type
================================================================================

let i: [Int].Index? = nil

--------------------------------------------------------------------------------

(source_file
  (property_declaration
    (value_binding_pattern)
    (pattern
      (simple_identifier))
    (type_annotation
      (optional_type
        (bracket_qualified_type
          (array_type
            (user_type
              (type_identifier)))
          (type_identifier))))
    (nil_literal)))
