Swift AST. Init, Deinit, Subscript.

Alexey Demedeckiy
3 min readApr 20, 2017

--

This is 8th part of my series about creating Swift AST in Swift. In this part I will cover special kinds of function-like declarations. You can find previous part here.

Init

In swift initializer is a common declaration, not required to be nested in class declaration.

It is following pattern from other declarations. We can flatten head into attributes, declarations and optional modifier, then we have generic parameters, regular parameters and generic constraints and code block with body.

struct InitializerDeclaration {
let attributes: [Attribute]
let modifiers: [DeclarationModifier]
let optionalModifier: InitializerOptionalKind?
let genericParamters: GenericParameterClause?
let parameters: [Parameter]
let throwBehavior: ThrowsModifier?
let `where`: GenericWhereClause?
let code: CodeBlock
}

This is 99% copy of protocol initializer declaration. Should it be refactored?

Deinit

This is much more simpler because you don’t have any arguments.

struct DeinitializerDeclaration {
let attributes: [Attribute]
let code: CodeBlock
}

Subscript

Subscript is like function but looks like it cannot throw. Also, I don’t see any signs of generic support. Isn’t it already implemented in latest Swift?

struct SubscriptDeclaration {
let attributes: [Attribute]
let modifiers: [DeclarationModifier]
let parameters: [Parameter]
let result: FunctionResult
let body: SubscriptBody
}
enum SubscriptBody {
case code(CodeBlock)
case getterSetter(GetterBlock, SetterBlock?)
case getterSetterKeyword(GetterKeywordClause, SetterKeywordClause?)
}

Looks like subscript declaration is some kind of mutant between function(can have a return type) and variable (can have getter and setter). But it cannot be marked as throws, and cannot have willSet and didSet. Probably the reason behind it — becouse subscript allow multiple paramters, it cannot be modeled as a variable.

Bonus level. Extensions

In addition to this function-like constructs in this part I will cover extensions.

There three different types of extension:

  • Simple type extension
  • Generic extension with specification
  • Inheritance extension

It can be modeled as an optional enum of 2 cases.

struct ExtensionDeclaration {
let attributes: [Attribute]
let accessModifier: AccessLevelModifier?
let type: Identifier
let specialization: ExtensionSpecicalization?
let members: [ExtensionMember]
}
enum ExtensionSpecialization {
case inheritance(TypeInheritanceClause)
case generic(GenericWhereClause)
}
enum ExtensionMember {
case declaration(Declaration)
case compilerStatement(CompilerControlStatement)
}

In next part I will cover operators and precedence group syntax — last parts of declaration group.

--

--