use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.
the class ArraySubscriptionExpression method typeChanged.
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
Type previous = this.type;
setType(getSubscriptType(src.getPropagationType()), root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.
the class BinaryOperator method typeChanged.
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
Type previous = this.type;
if (this.operatorCode.equals("=")) {
setType(src.getPropagationType(), root);
} else {
if (this.lhs != null && "java.lang.String".equals(this.lhs.getType().toString()) || this.rhs != null && "java.lang.String".equals(this.rhs.getType().toString())) {
getPossibleSubTypes().clear();
setType(TypeParser.createFrom("java.lang.String", true), root);
}
}
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.
the class ConditionalExpression method typeChanged.
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
Type previous = this.type;
List<Type> types = new ArrayList<>();
if (thenExpr != null && thenExpr.getPropagationType() != null) {
types.add(thenExpr.getPropagationType());
}
if (elseExpr != null && elseExpr.getPropagationType() != null) {
types.add(elseExpr.getPropagationType());
}
Set<Type> subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.remove(oldType);
subTypes.addAll(types);
Type alternative = !types.isEmpty() ? types.get(0) : UnknownType.getUnknownType();
setType(TypeManager.getInstance().getCommonType(types).orElse(alternative), root);
setPossibleSubTypes(subTypes, root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.
the class FieldDeclaration method typeChanged.
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
if (!TypeManager.isTypeSystemActive()) {
return;
}
if (!TypeManager.getInstance().isUnknown(this.type) && src.getPropagationType().equals(oldType)) {
return;
}
Type previous = this.type;
Type newType;
if (src == initializer && initializer instanceof InitializerListExpression) {
// can be ignored once we have a type
if (isArray) {
newType = src.getType();
} else if (!TypeManager.getInstance().isUnknown(this.type)) {
return;
} else {
newType = src.getType().dereference();
}
} else {
newType = src.getPropagationType();
}
setType(newType, root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Type.Origin.DATAFLOW);
}
}
use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.
the class SubgraphWalker method activateTypes.
public static void activateTypes(Node node, ScopeManager scopeManager) {
AtomicInteger num = new AtomicInteger();
Map<HasType, Set<Type>> typeCache = TypeManager.getInstance().getTypeCache();
IterativeGraphWalker walker = new IterativeGraphWalker();
walker.registerOnNodeVisit(scopeManager::enterScopeIfExists);
walker.registerOnScopeExit(n -> {
if (n instanceof HasType) {
HasType typeNode = (HasType) n;
typeCache.getOrDefault(typeNode, Collections.emptySet()).forEach(t -> {
t = TypeManager.getInstance().resolvePossibleTypedef(t);
((HasType) n).setType(t);
});
typeCache.remove((HasType) n);
num.getAndIncrement();
}
});
walker.iterate(node);
LOGGER.debug("Activated {} nodes for {}", num, node.getName());
// For some nodes it may happen that they are not reachable via AST, but we still need to set
// their type to the requested value
typeCache.forEach((n, types) -> types.forEach(t -> {
t = TypeManager.getInstance().resolvePossibleTypedef(t);
n.setType(t);
}));
}
Aggregations