Search in sources :

Example 16 with HasType

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

the class VariableDeclaration 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)

Example 17 with HasType

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

Example 18 with HasType

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

the class UnaryOperator method typeChanged.

@Override
public void typeChanged(HasType src, Collection<HasType> root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    if (src == input) {
        Type newType = src.getPropagationType();
        if (operatorCode.equals("*")) {
            newType = newType.dereference();
        } else if (operatorCode.equals("&")) {
            newType = newType.reference(PointerType.PointerOrigin.POINTER);
        }
        setType(newType, root);
    } else {
        // Our input didn't change, so we don't need to (de)reference the type
        setType(src.getPropagationType(), root);
        // Pass the type on to the input in an inversely (de)referenced way
        Type newType = src.getPropagationType();
        if (operatorCode.equals("*")) {
            newType = src.getPropagationType().reference(PointerType.PointerOrigin.POINTER);
        } else if (operatorCode.equals("&")) {
            newType = src.getPropagationType().dereference();
        }
        // not null
        if (input != null) {
            input.setType(newType, new ArrayList<>(List.of(this)));
        }
    }
    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) PointerType(de.fraunhofer.aisec.cpg.graph.types.PointerType)

Example 19 with HasType

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

the class ValueDeclaration method setType.

@Override
public void setType(Type type, HasType root) {
    if (!TypeManager.isTypeSystemActive()) {
        TypeManager.getInstance().cacheType(this, type);
        return;
    }
    if (type == null || root == this) {
        return;
    }
    if (this.type instanceof FunctionPointerType && !(type instanceof FunctionPointerType)) {
        return;
    }
    Type oldType = this.type;
    if (TypeManager.getInstance().isUnknown(type)) {
        return;
    }
    type = type.duplicate();
    type.setQualifier(this.type.getQualifier().merge(type.getQualifier()));
    Set<Type> subTypes = new HashSet<>();
    for (Type t : getPossibleSubTypes()) {
        if (!t.isSimilar(type)) {
            subTypes.add(t);
        }
    }
    subTypes.add(type);
    this.type = TypeManager.getInstance().registerType(TypeManager.getInstance().getCommonType(subTypes).orElse(type));
    subTypes = subTypes.stream().filter(s -> TypeManager.getInstance().isSupertypeOf(this.type, s)).collect(Collectors.toSet());
    subTypes = subTypes.stream().map(s -> TypeManager.getInstance().registerType(s)).collect(Collectors.toSet());
    setPossibleSubTypes(subTypes);
    if (!Objects.equals(oldType, type)) {
        this.typeListeners.stream().filter(l -> !l.equals(this)).forEach(l -> l.typeChanged(this, root == null ? this : root, oldType));
    }
}
Also used : FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) java.util(java.util) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) TypeManager(de.fraunhofer.aisec.cpg.graph.TypeManager) Type(de.fraunhofer.aisec.cpg.graph.types.Type) Predicate(java.util.function.Predicate) Transient(org.neo4j.ogm.annotation.Transient) Collectors(java.util.stream.Collectors) HasType(de.fraunhofer.aisec.cpg.graph.HasType) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Node(de.fraunhofer.aisec.cpg.graph.Node) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType) NotNull(org.jetbrains.annotations.NotNull) UnknownType(de.fraunhofer.aisec.cpg.graph.types.UnknownType) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType) ReferenceType(de.fraunhofer.aisec.cpg.graph.types.ReferenceType) FunctionPointerType(de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType)

Example 20 with HasType

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

the class ArrayCreationExpression method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    Type previous = this.type;
    setType(src.getPropagationType(), 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)

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