Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ module DataFlowInput implements InputSig<Location> {
private predicate isParameterNodeImpl(Node p, DataFlowCallable c, ParameterPosition pos) {
exists(Parameter param |
p.asExpr() = param.getPattern() and
c.asSourceCallable() = param.getEnclosingCallable()
|
pos.asPositional() = param.getPositionalIndex()
or
pos.asNamed() = param.getExternalName()
c.asSourceCallable() = param.getEnclosingCallable() and
pos = getParameterPosition(param)
)
or
p.(BuilderNode).isImplicitParameter(c, pos)
Expand All @@ -102,11 +99,8 @@ module DataFlowInput implements InputSig<Location> {
private predicate isArgumentNodeImpl(Node n, DataFlowCall call, ArgumentPosition pos) {
exists(Argument arg |
n.asExpr() = arg.getValue() and
call.asExplicitCall().getAnArgument() = arg
|
pos.asPositional() = arg.getPositionalIndex()
or
pos.asNamed() = arg.getName()
call.asExplicitCall().getAnArgument() = arg and
pos = getArgumentPosition(_, arg)
)
or
n.(BuilderNode).isImplicitArgument(call, pos, false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
private import unified

private int getExternalNameRank(Callable c, Parameter p, string name) {
p =
rank[result](Parameter p0, int pos |
p0 = c.getParameter(pos) and
name = p0.getExternalName()
|
p0 order by pos
)
}

private predicate parameterPos(Callable c, Parameter p, int pos, string name, int rnk) {
p = c.getParameter(pos) and
(
rnk = getExternalNameRank(c, p, name)
or
not exists(p.getExternalName()) and
name = "" and
rnk = 1
)
}

private newtype TParameterPosition =
TReceiverParameter() or
TCalleeParameter() or
TPositionalParameter(int n) { n = [0 .. 20] } or
TNamedParameter(string name) {
name = any(Parameter p).getExternalName()
or
name = any(Argument arg).getName()
}
TSourceParameter(int pos, string name, int rnk) { parameterPos(_, _, pos, name, rnk) } or
TSummaryPositionalParameter(int pos) { pos = [0 .. 20] } or
TSummaryNamedParameter(string name) { name = any(Argument arg).getName() }

class ParameterPosition extends TParameterPosition {
/** Holds if this represents the receiver passed to a call (usually called `this` or `self`). */
Expand All @@ -17,11 +35,89 @@
/** Holds if this represents the function value being invoked in a call. */
predicate isCallee() { this = TCalleeParameter() }

int asPositional() { this = TPositionalParameter(result) }
/**
* Holds if this represents a parameter from source code at postion `pos` and
* with external name `name` and rank `rnk`.
*
* `name` is `""` when there is no external name, and `rnk` is the rank of the
* parameter among parameters with the same external name.
*/
Comment on lines +38 to +44
predicate isSourceParameter(int pos, string name, int rnk) {
this = TSourceParameter(pos, name, rnk)
}

/** Gets the represented positional summary parameter index, if any. */
int asPositionalSummaryParameter() { this = TSummaryPositionalParameter(result) }

/** Gets the represented named summary parameter, if any. */
string asNamedSummaryParameter() { this = TSummaryNamedParameter(result) }

/** Gets the operand to `Argument` that represents this position in a MaD model. */
string asModelOperand() {
this.isReceiver() and result = "self"
or
this.isCallee() and result = "callee"
or
result = this.asPositionalSummaryParameter().toString()
or
// Suffix with a colon to prevent a confusing name clash with "receiver". This also aligns with MaD syntax.
result = this.asNamedSummaryParameter() + ":"
}

/** Gets a textual representation of this position. */
string toString() {
result = this.asModelOperand()
or
exists(int pos, string name | this.isSourceParameter(pos, name, _) |
if name = ""
then result = pos.toString()
else
// Suffix with a colon to prevent a confusing name clash with "receiver". This also aligns with MaD syntax.
result = name + ":"
)
}
}

ParameterPosition getParameterPosition(Parameter p) {
exists(int pos, string name, int rnk |
parameterPos(_, p, pos, name, rnk) and
result = TSourceParameter(pos, name, rnk)
)
}

private predicate positionalArgument(CallExpr ce, Argument arg, int pos) {
arg = ce.getArgument(pos) and
not exists(arg.getName())
}

private int getNameRank(CallExpr ce, Argument arg, string name) {
arg =
rank[result](Argument arg0, int pos |
arg0 = ce.getArgument(pos) and
name = arg0.getName()
|
arg0 order by pos
)
}

string asNamed() { this = TNamedParameter(result) }
private newtype TArgumentPosition =
TReceiverArgument() or
TCalleeArgument() or
TPositionalArgument(int pos) { positionalArgument(_, _, pos) } or
TNamedArgument(string name, int rnk) { rnk = getNameRank(_, _, name) }

/** Gets the operand to `Parameter`/`Argument` that represents this position in a MaD model. */
class ArgumentPosition extends TArgumentPosition {
/** Holds if this represents the receiver passed to a call (usually called `this` or `self`). */
predicate isReceiver() { this = TReceiverArgument() }

/** Holds if this represents the function value being invoked in a call. */
predicate isCallee() { this = TCalleeArgument() }

int asPositional() { this = TPositionalArgument(result) }

string asNamed(int rnk) { this = TNamedArgument(result, rnk) }

/** Gets the operand to `Parameter` that represents this position in a MaD model. */
string asModelOperand() {
this.isReceiver() and result = "self"
or
Expand All @@ -30,21 +126,40 @@
result = this.asPositional().toString()
or
// Suffix with a colon to prevent a confusing name clash with "receiver". This also aligns with MaD syntax.
result = this.asNamed() + ":"
result = this.asNamed(_) + ":"
}

/** Gets a string representation of this position. */
/** Gets a textual representation of this position. */
string toString() { result = this.asModelOperand() }
}

class ArgumentPosition = ParameterPosition;
pragma[nomagic]
ArgumentPosition getArgumentPosition(CallExpr ce, Argument arg) {
positionalArgument(ce, arg, result.asPositional())
or
exists(string name | result.asNamed(getNameRank(ce, arg, name)) = name)
}

/** A parameter position that is either `receiver` or `callee`. */
class ImplicitParameterPosition extends ParameterPosition {
ImplicitParameterPosition() { this.isReceiver() or this.isCallee() }
}

/** An argument position that is either `receiver` or `callee`. */
class ImplicitArgumentPosition = ImplicitParameterPosition;
class ImplicitArgumentPosition extends ArgumentPosition {
ImplicitArgumentPosition() { this.isReceiver() or this.isCallee() }
}

predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) { apos = ppos }
predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) {
ppos.isReceiver() and apos.isReceiver()
or
ppos.isCallee() and apos.isCallee()
or
ppos = TSourceParameter(apos.asPositional(), _, _)
or
ppos = TSummaryPositionalParameter(apos.asPositional())
or
exists(int rnk | ppos = TSourceParameter(_, apos.asNamed(rnk), rnk))
or
ppos = TSummaryNamedParameter(apos.asNamed(_))
}

This file was deleted.

Loading