Search in sources :

Example 6 with HasType

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, Collection<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);
    }
}
Also used : HasType(de.fraunhofer.aisec.cpg.graph.HasType) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type)

Example 7 with HasType

use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.

the class ExpressionList method addExpression.

public void addExpression(Statement expression) {
    if (!this.expressions.isEmpty()) {
        Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
        if (lastExpression instanceof HasType)
            ((HasType) lastExpression).unregisterTypeListener(this);
        this.removePrevDFG(lastExpression);
    }
    PropertyEdge<Statement> propertyEdge = new PropertyEdge<>(this, expression);
    propertyEdge.addProperty(Properties.INDEX, this.expressions.size());
    this.expressions.add(propertyEdge);
    this.addPrevDFG(expression);
    if (expression instanceof HasType) {
        ((HasType) expression).registerTypeListener(this);
    }
}
Also used : Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) HasType(de.fraunhofer.aisec.cpg.graph.HasType) PropertyEdge(de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)

Example 8 with HasType

use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.

the class ExpressionList method typeChanged.

@Override
public void typeChanged(HasType src, Collection<HasType> root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    setType(src.getPropagationType(), root);
    setPossibleSubTypes(new HashSet<>(src.getPossibleSubTypes()), root);
    if (!previous.equals(this.type)) {
        this.type.setTypeOrigin(Type.Origin.DATAFLOW);
    }
}
Also used : HasType(de.fraunhofer.aisec.cpg.graph.HasType) Type(de.fraunhofer.aisec.cpg.graph.types.Type)

Example 9 with HasType

use of de.fraunhofer.aisec.cpg.graph.HasType in project cpg by Fraunhofer-AISEC.

the class ExpressionList method setExpressions.

public void setExpressions(List<Statement> expressions) {
    if (!this.expressions.isEmpty()) {
        Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
        if (lastExpression instanceof HasType)
            ((HasType) lastExpression).unregisterTypeListener(this);
        this.removePrevDFG(lastExpression);
    }
    this.expressions = PropertyEdge.transformIntoOutgoingPropertyEdgeList(expressions, this);
    if (!this.expressions.isEmpty()) {
        Statement lastExpression = this.expressions.get(this.expressions.size() - 1).getEnd();
        this.addPrevDFG(lastExpression);
        if (lastExpression instanceof HasType)
            ((HasType) lastExpression).registerTypeListener(this);
    }
}
Also used : Statement(de.fraunhofer.aisec.cpg.graph.statements.Statement) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 10 with HasType

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, Collection<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);
    }
}
Also used : Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) InitializerListExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression)

Aggregations

HasType (de.fraunhofer.aisec.cpg.graph.HasType)26 Type (de.fraunhofer.aisec.cpg.graph.types.Type)23 UnknownType (de.fraunhofer.aisec.cpg.graph.types.UnknownType)7 Node (de.fraunhofer.aisec.cpg.graph.Node)5 TypeManager (de.fraunhofer.aisec.cpg.graph.TypeManager)5 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 PropertyEdge (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge)4 InitializerListExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.InitializerListExpression)4 ToStringBuilder (org.apache.commons.lang3.builder.ToStringBuilder)4 TypeListener (de.fraunhofer.aisec.cpg.graph.HasType.TypeListener)3 SubGraph (de.fraunhofer.aisec.cpg.graph.SubGraph)3 FunctionPointerType (de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)3 ReferenceType (de.fraunhofer.aisec.cpg.graph.types.ReferenceType)3 Predicate (java.util.function.Predicate)3 Relationship (org.neo4j.ogm.annotation.Relationship)3 FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)2 Properties (de.fraunhofer.aisec.cpg.graph.edge.Properties)2 PropertyEdge.unwrap (de.fraunhofer.aisec.cpg.graph.edge.PropertyEdge.unwrap)2 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)2