de.fraunhofer.aisec.cpg.graph.MemberExpression Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cpg Show documentation
Show all versions of cpg Show documentation
A simple library to extract a code property graph out of source code. It has support for multiple passes that can extend the analysis after the graph is constructed.
/*
* Copyright (c) 2019, Fraunhofer AISEC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $$$$$$\ $$$$$$$\ $$$$$$\
* $$ __$$\ $$ __$$\ $$ __$$\
* $$ / \__|$$ | $$ |$$ / \__|
* $$ | $$$$$$$ |$$ |$$$$\
* $$ | $$ ____/ $$ |\_$$ |
* $$ | $$\ $$ | $$ | $$ |
* \$$$$$ |$$ | \$$$$$ |
* \______/ \__| \______/
*
*/
package de.fraunhofer.aisec.cpg.graph;
import de.fraunhofer.aisec.cpg.graph.HasType.TypeListener;
import de.fraunhofer.aisec.cpg.graph.Type.Origin;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Represents access to a field of a {@link RecordDeclaration}, such as obj.property
.
*/
public class MemberExpression extends Expression implements TypeListener {
@SubGraph("AST")
protected Node member;
@SubGraph("AST")
@NonNull
private Node base;
/**
* Is this reference used for writing data instead of just reading it? Determines dataflow
* direction
*/
private boolean writingAccess = false;
@NonNull
public Node getBase() {
return base;
}
public void setBase(@NonNull Node base) {
this.base = base;
}
public Node getMember() {
return member;
}
public void setMember(Node member) {
if (this.member instanceof TypeListener) {
unregisterTypeListener((TypeListener) this.member);
}
if (this.member instanceof HasType) {
((HasType) this.member).unregisterTypeListener(this);
}
if (this.writingAccess) {
this.removeNextDFG(this.member);
} else {
this.removePrevDFG(this.member);
}
this.member = member;
if (member instanceof HasType) {
((HasType) member).registerTypeListener(this);
}
if (member instanceof TypeListener) {
registerTypeListener((TypeListener) member);
}
if (this.writingAccess) {
this.addNextDFG(this.member);
} else {
this.addPrevDFG(this.member);
}
}
@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
Type previous = this.type;
setType(src.getType(), root);
if (!previous.equals(this.type)) {
this.type.setTypeOrigin(Origin.DATAFLOW);
}
}
@Override
public void possibleSubTypesChanged(HasType src, HasType root, Set oldSubTypes) {
Set subTypes = new HashSet<>(getPossibleSubTypes());
subTypes.addAll(src.getPossibleSubTypes());
setPossibleSubTypes(subTypes, root);
}
@Override
public String toString() {
return new ToStringBuilder(this, Node.TO_STRING_STYLE)
.appendSuper(super.toString())
.append("base", base)
.append("member", member)
.toString();
}
public void setWritingAccess(boolean writingAccess) {
if (this.writingAccess) {
this.removeNextDFG(this.member);
} else {
this.removePrevDFG(this.member);
}
this.writingAccess = writingAccess;
if (this.writingAccess) {
this.addNextDFG(this.member);
} else {
this.addPrevDFG(this.member);
}
}
public boolean isWritingAccess() {
return writingAccess;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof MemberExpression)) {
return false;
}
MemberExpression that = (MemberExpression) o;
return super.equals(that)
&& Objects.equals(member, that.member)
&& Objects.equals(base, that.base);
}
@Override
public int hashCode() {
return super.hashCode();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy