PS: Move a bunch of predicates into ScriptBlock.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-10-16 20:11:49 +01:00
parent d3b9e139c4
commit 56c703ec80
2 changed files with 42 additions and 28 deletions

View File

@@ -10,7 +10,7 @@ class ScriptBlock extends @script_block, Ast {
else result = "{...}"
}
override SourceLocation getLocation() { script_block_location(this, result) }
override Location getLocation() { script_block_location(this, result) }
int getNumUsings() { script_block(this, result, _, _, _, _) }
@@ -51,6 +51,42 @@ class ScriptBlock extends @script_block, Ast {
ModuleSpecification getAModuleSpecification() { result = this.getModuleSpecification(_) }
final override Scope getEnclosingScope() { result = this }
/**
* Gets the `i`'th paramter in this scope.
*
* This may be both function paramters and parameter block parameters.
*/
Parameter getParameter(int i) {
exists(Function func |
func.getBody() = this and
result = func.getParameter(i)
)
or
this.isTopLevel() and
result = this.getParamBlock().getParameter(i)
}
/**
* Gets a paramter in this scope.
*
* This may be both function parameters and parameter block parameters.
*/
Parameter getAParameter() { result = this.getParameter(_) }
Parameter getThisParameter() {
exists(Function func |
func.getBody() = this and
result = func.getThisParameter()
)
}
/** Gets the number of function parameters. */
final int getNumberOfParameters() { result = count(this.getAParameter()) }
final Parameter getParameterExcludingPiplines(int i) {
result = this.getParamBlock().getParameterExcludingPiplines(i)
}
}
/** A `process` block. */
@@ -69,3 +105,7 @@ class ProcessBlock extends NamedBlock {
result = scriptBlock.getEnclosingFunction().getAParameter()
}
}
class TopLevelScriptBlock extends ScriptBlock {
TopLevelScriptBlock() { this.isTopLevel() }
}

View File

@@ -14,33 +14,7 @@ Scope scopeOf(Ast n) {
* A variable scope. This is either a top-level (file), a module, a class,
* or a callable.
*/
class Scope extends Ast, @script_block {
class Scope extends Ast, ScriptBlock {
/** Gets the outer scope, if any. */
Scope getOuterScope() { result = scopeOf(this) }
/**
* Gets the `i`'th paramter in this scope.
*
* This may be both function paramters and parameter block parameters.
*/
Parameter getParameter(int i) {
exists(Function func |
func.getBody() = this and
result = func.getParameter(i)
)
}
/**
* Gets a paramter in this scope.
*
* This may be both function paramters and parameter block parameters.
*/
Parameter getAParameter() { result = this.getParameter(_) }
Parameter getThisParameter() {
exists(Function func |
func.getBody() = this and
result = func.getThisParameter()
)
}
}