Skip to content

Commit 32f7f1b

Browse files
committed
PS: Merge the non-member function and member function classes. Also rename member function to 'Method' since that's the name used by Powershell documentation.
1 parent 5103d34 commit 32f7f1b

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

powershell/ql/lib/semmle/code/powershell/Function.qll

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,58 @@ import powershell
22
import semmle.code.powershell.controlflow.BasicBlocks
33

44
abstract private class AbstractFunction extends Ast {
5+
/** Gets the name of this function. */
56
abstract string getName();
67

8+
/** Holds if this function has name `name`. */
79
final predicate hasName(string name) { this.getName() = name }
810

11+
/** Gets the body of this function. */
912
abstract ScriptBlock getBody();
1013

14+
/**
15+
* Gets the i'th function parameter, if any.
16+
*
17+
* Note that this predicate only returns _function_ parameters.
18+
* To also get _block_ parameters use the `getParameter` predicate.
19+
*/
1120
abstract Parameter getFunctionParameter(int i);
1221

22+
/** Gets the declaring type of this function, if any. */
23+
abstract Type getDeclaringType();
24+
25+
/**
26+
* Gets any function parameter of this function.
27+
*
28+
* Note that this only gets _function_ paramters. To get any parameter
29+
* use the `getAParameter` predicate.
30+
*/
1331
final Parameter getAFunctionParameter() { result = this.getFunctionParameter(_) }
1432

33+
/** Gets the number of function parameters. */
1534
final int getNumberOfFunctionParameters() { result = count(this.getAFunctionParameter()) }
1635

36+
/** Gets the number of parameters (both function and block). */
1737
final int getNumberOfParameters() { result = count(this.getAParameter()) }
1838

39+
/** Gets the i'th parameter of this function, if any. */
1940
final Parameter getParameter(int i) {
2041
result = this.getFunctionParameter(i)
2142
or
2243
result = this.getBody().getParamBlock().getParameter(i)
2344
}
2445

46+
/** Gets any parameter of this function. */
2547
final Parameter getAParameter() { result = this.getParameter(_) }
2648

49+
/** Gets the entry point of this function in the control-flow graph. */
2750
EntryBasicBlock getEntryBasicBlock() { result.getScope() = this.getBody() }
2851
}
2952

30-
class NonMemberFunction extends @function_definition, Stmt, AbstractFunction {
53+
/**
54+
* A function definition.
55+
*/
56+
private class FunctionBase extends @function_definition, Stmt, AbstractFunction {
3157
override string toString() { result = this.getName() }
3258

3359
override SourceLocation getLocation() { function_definition_location(this, result) }
@@ -41,34 +67,32 @@ class NonMemberFunction extends @function_definition, Stmt, AbstractFunction {
4167
predicate isWorkflow() { function_definition(this, _, _, _, true) }
4268

4369
override Parameter getFunctionParameter(int i) { result.isFunctionParameter(this, i) }
44-
}
45-
46-
class MemberFunction extends @function_member, Member, AbstractFunction {
47-
override string getName() { function_member(this, _, _, _, _, _, _, result, _) }
48-
49-
override SourceLocation getLocation() { function_member_location(this, result) }
50-
51-
override string toString() { result = this.getName() }
5270

53-
override ScriptBlock getBody() { function_member(this, result, _, _, _, _, _, _, _) }
54-
55-
override predicate isHidden() { function_member(this, _, _, true, _, _, _, _, _) }
56-
57-
override predicate isPrivate() { function_member(this, _, _, _, true, _, _, _, _) }
71+
override Type getDeclaringType() { none() }
72+
}
5873

59-
override predicate isPublic() { function_member(this, _, _, _, _, true, _, _, _) }
74+
private predicate isMethod(Member m, ScriptBlock body) {
75+
function_member(m, body, _, _, _, _, _, _, _)
76+
}
6077

61-
override predicate isStatic() { function_member(this, _, _, _, _, _, true, _, _) }
78+
/**
79+
* A method definition. That is, a function defined inside a class definition.
80+
*/
81+
class Method extends FunctionBase {
82+
Method() { isMethod(_, super.getBody()) }
6283

63-
predicate isConstructor() { function_member(this, _, true, _, _, _, _, _, _) }
84+
/** Gets the member corresponding to this function definition. */
85+
Member getMember() { isMethod(result, super.getBody()) }
6486

65-
override Parameter getFunctionParameter(int i) { result.isFunctionParameter(this, i) }
87+
/** Holds if this method is a constructor. */
88+
predicate isConstructor() { function_member(this.getMember(), _, true, _, _, _, _, _, _) }
6689

67-
TypeConstraint getTypeConstraint() { function_member_return_type(this, result) }
90+
final override Type getDeclaringType() { result = this.getMember().getDeclaringType() }
6891
}
6992

70-
class Constructor extends MemberFunction {
93+
/** A constructor definition. */
94+
class Constructor extends Method {
7195
Constructor() { this.isConstructor() }
7296
}
7397

74-
final class Function = AbstractFunction;
98+
final class Function = FunctionBase;

powershell/ql/lib/semmle/code/powershell/Type.qll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ class Type extends @type_definition, Stmt {
1111

1212
Member getAMember() { result = this.getMember(_) }
1313

14-
MemberFunction getMemberFunction(string name) {
15-
result = this.getAMember() and
14+
Method getMethod(string name) {
15+
result.getMember() = this.getAMember() and
1616
result.hasName(name)
1717
}
1818

19-
MemberFunction getAMemberFunction() { result = this.getMemberFunction(_) }
19+
Constructor getAConstructor() {
20+
result = this.getAMethod() and
21+
result.getName() = this.getName()
22+
}
23+
24+
Method getAMethod() { result = this.getMethod(_) }
2025
}

powershell/ql/lib/semmle/code/powershell/Variable.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ private import internal.Internal as Internal
44

55
private predicate isFunctionParameterImpl(Internal::Parameter p, Function f, int i) {
66
function_definition_parameter(f, i, p)
7-
or
8-
function_member_parameter(f, i, p)
97
}
108

119
private predicate hasParameterBlockImpl(Internal::Parameter p, ParamBlock block, int i) {

0 commit comments

Comments
 (0)