C#: Speedup Element::getLabel()

This commit is contained in:
Tom Hvitved
2019-02-19 15:30:33 +01:00
parent c70a0a646d
commit 4054dc43e2
3 changed files with 81 additions and 25 deletions

View File

@@ -27,24 +27,64 @@ class Callable extends Declaration, @dotnet_callable {
/** Holds if this callable can return expression `e`. */
predicate canReturn(Expr e) { none() }
pragma[noinline]
private string getDeclaringTypeLabel() { result = this.getDeclaringType().getLabel() }
pragma[noinline]
private string getParameterTypeLabelNonGeneric(int p) {
not this instanceof Generic and
result = this.getParameter(p).getType().getLabel()
}
language[monotonicAggregates]
pragma[nomagic]
private string getMethodParamListNonGeneric() {
result = concat(int p |
p in [0 .. this.getNumberOfParameters() - 1]
|
this.getParameterTypeLabelNonGeneric(p), "," order by p
)
}
pragma[noinline]
private string getParameterTypeLabelGeneric(int p) {
this instanceof Generic and
result = this.getParameter(p).getType().getLabel()
}
language[monotonicAggregates]
pragma[nomagic]
private string getMethodParamListGeneric() {
result = concat(int p |
p in [0 .. this.getNumberOfParameters() - 1]
|
this.getParameterTypeLabelGeneric(p), "," order by p
)
}
pragma[noinline]
private string getLabelNonGeneric() {
not this instanceof Generic and
result = this.getReturnTypeLabel() + " " + this.getDeclaringTypeLabel() + "." +
this.getUndecoratedName() + "(" + this.getMethodParamListNonGeneric() + ")"
}
pragma[noinline]
private string getLabelGeneric() {
result = this.getReturnTypeLabel() + " " + this.getDeclaringTypeLabel() + "." +
this.getUndecoratedName() + getGenericsLabel(this) + "(" + this.getMethodParamListGeneric() +
")"
}
final override string getLabel() {
result = getReturnTypeLabel() + " " + getDeclaringType().getLabel() + "." + getUndecoratedName()
+ getGenericsLabel(this) + getMethodParams()
result = this.getLabelNonGeneric() or
result = this.getLabelGeneric()
}
private string getReturnTypeLabel() {
if exists(getReturnType()) then result = getReturnType().getLabel() else result = "System.Void"
}
private string getMethodParams() { result = "(" + getMethodParamList() + ")" }
language[monotonicAggregates]
private string getMethodParamList() {
result = concat(int p |
exists(getParameter(p))
|
getParameter(p).getType().getLabel(), "," order by p
)
result = getReturnType().getLabel()
or
not exists(this.getReturnType()) and result = "System.Void"
}
override string getUndecoratedName() { result = getName() }

View File

@@ -66,19 +66,23 @@ abstract class ConstructedGeneric extends Generic {
*
* Constructs the label suffix for a generic method or type.
*/
string getGenericsLabel(Declaration d) {
result = "`" + d.(UnboundGeneric).getNumberOfTypeParameters()
string getGenericsLabel(Generic g) {
result = "`" + g.(UnboundGeneric).getNumberOfTypeParameters()
or
result = "<" + typeArgs(d) + ">"
or
not d instanceof Generic and result = ""
result = "<" + typeArgs(g) + ">"
}
pragma[noinline]
private string getTypeArgumentLabel(ConstructedGeneric generic, int p) {
result = generic.getTypeArgument(p).getLabel()
}
language[monotonicAggregates]
pragma[nomagic]
private string typeArgs(ConstructedGeneric generic) {
result = concat(int p |
exists(generic.getTypeArgument(p))
p in [0 .. generic.getNumberOfTypeArguments() - 1]
|
generic.getTypeArgument(p).getLabel(), ","
getTypeArgumentLabel(generic, p), ","
)
}

View File

@@ -23,10 +23,6 @@ class ValueOrRefType extends Type, @dotnet_valueorreftype {
/** Gets the namespace declaring this type, if any. */
Namespace getDeclaringNamespace() { none() }
override string getLabel() {
result = getPrefixWithTypes() + getUndecoratedName() + getGenericsLabel(this)
}
private string getPrefixWithTypes() {
result = getDeclaringType().getLabel() + "."
or
@@ -35,6 +31,22 @@ class ValueOrRefType extends Type, @dotnet_valueorreftype {
else result = getDeclaringNamespace().getQualifiedName() + "."
}
pragma[noinline]
private string getLabelNonGeneric() {
not this instanceof Generic and
result = this.getPrefixWithTypes() + this.getUndecoratedName()
}
pragma[noinline]
private string getLabelGeneric() {
result = this.getPrefixWithTypes() + this.getUndecoratedName() + getGenericsLabel(this)
}
override string getLabel() {
result = this.getLabelNonGeneric() or
result = this.getLabelGeneric()
}
/** Gets a base type of this type, if any. */
ValueOrRefType getABaseType() { none() }
}