Swift: Extract self parameter declarations.

This commit is contained in:
Mathias Vorreiter Pedersen
2022-08-23 15:49:02 +01:00
parent 4df2e5d937
commit 273053c92a
3 changed files with 18 additions and 1 deletions

View File

@@ -81,6 +81,7 @@ AstNode:
Callable:
_children:
params: ParamDecl*
self_param: ParamDecl?
body: BraceStmt?
ConditionElement:

View File

@@ -327,6 +327,8 @@ void DeclVisitor::fillAbstractFunctionDecl(const swift::AbstractFunctionDecl& de
entry.name = !decl.hasName() ? "(unnamed function decl)" : constructName(decl.getName());
entry.body = dispatcher_.fetchOptionalLabel(decl.getBody());
entry.params = dispatcher_.fetchRepeatedLabels(*decl.getParameters());
auto self = const_cast<swift::ParamDecl* const>(decl.getImplicitSelfDecl());
entry.self_param = dispatcher_.fetchOptionalLabel(self);
fillValueDecl(decl, entry);
fillGenericContext(decl, entry);
}

View File

@@ -5,6 +5,20 @@ class ParamDecl extends ParamDeclBase {
/** Gets the function which declares this parameter. */
Callable getDeclaringFunction() { result.getAParam() = this }
/** Gets the index of this parameter in its declaring function's parameter list. */
/**
* Gets the index of this parameter in its declaring function's parameter list,
* or -1 if this is `self`.
*/
int getIndex() { exists(Callable func | func.getParam(result) = this) }
}
/** A `self` parameter. */
class SelfParamDecl extends ParamDecl {
Callable call;
SelfParamDecl() { call.getSelfParam() = this }
override Callable getDeclaringFunction() { result = call }
override int getIndex() { result = -1 }
}