Skip to content

Commit

Permalink
Make sure AST is available to C getExpressionType too
Browse files Browse the repository at this point in the history
C++ parser methods already store current lookup point which makes AST available
via thread-local variable. Do the same for C parser to enable SizeofCalculator
accessing type size macros via AST while processing C code.
  • Loading branch information
i-garrison authored and jonahgraham committed Feb 4, 2023
1 parent b84b0f6 commit c973dd5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ private static long getApproximateSize(IBasicType type) {

/**
* Checks whether a target integral type can represent all values of a source integral type.
* <br><br>
* If CPPSemantics current lookup point is set, size and alignment information is derived
* from predefined type size macros available through current AST. If there is no current
* lookup point, {@link #getApproximateSize(IBasicType)} is used to guess size of integral types.
*
* @param target the target integral type
* @param source the source integral type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;

/**
* Binary expression for c
Expand Down Expand Up @@ -213,6 +214,15 @@ public void replace(IASTNode child, IASTNode other) {

@Override
public IType getExpressionType() {
CPPSemantics.pushLookupPoint(this);
try {
return getExpressionTypeImpl();
} finally {
CPPSemantics.popLookupPoint();
}
}

private IType getExpressionTypeImpl() {
final int op = getOperator();
IType originalType1 = getOperand1().getExpressionType();
IType originalType2 = getOperand2().getExpressionType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes;

/**
Expand Down Expand Up @@ -163,6 +164,15 @@ public void replace(IASTNode child, IASTNode other) {

@Override
public IType getExpressionType() {
CPPSemantics.pushLookupPoint(this);
try {
return getExpressionTypeImpl();
} finally {
CPPSemantics.popLookupPoint();
}
}

private IType getExpressionTypeImpl() {
IASTExpression positiveExpression = getPositiveResultExpression();
if (positiveExpression == null) {
positiveExpression = getLogicalConditionExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
Expand Down Expand Up @@ -323,12 +324,15 @@ public static void popLookupPoint() {

/**
* Get the current point of instantiation / point of lookup for name lookups.
*
* <br><br>
* NOTE: This is meant to be used primarily for "declaredBefore" purposes, that is,
* for determining whether something was declared before or after the point
* of lookup. It is NOT meant to be used as a general mechanism for accessing
* information about a call site without having to pass that information along
* the usual way (via function arguments).
* <br><br>
* NOTE: This is also used to provide {@link SizeofCalculator} with access to predefined
* type size macros of current translation unit via AST object.
*/
public static IASTNode getCurrentLookupPoint() {
Deque<IASTNode> lookupPoints = fLookupPoints.get();
Expand Down

0 comments on commit c973dd5

Please sign in to comment.