Skip to content

Commit

Permalink
variable declaration and reference now store null if type was not spe…
Browse files Browse the repository at this point in the history
…cified, but in that case it is still treated as item* or with static analysis a proper type is inferred
  • Loading branch information
mario-arduini committed Oct 24, 2020
1 parent 55b3d71 commit 099221f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
31 changes: 28 additions & 3 deletions src/main/java/org/rumbledb/compiler/InferTypeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.rumbledb.expressions.logic.AndExpression;
import org.rumbledb.expressions.logic.NotExpression;
import org.rumbledb.expressions.logic.OrExpression;
import org.rumbledb.expressions.module.VariableDeclaration;
import org.rumbledb.expressions.primary.*;
import org.rumbledb.expressions.typing.CastExpression;
import org.rumbledb.expressions.typing.CastableExpression;
Expand Down Expand Up @@ -125,10 +126,13 @@ public StaticContext visitBoolean(BooleanLiteralExpression expression, StaticCon

@Override
public StaticContext visitVariableReference(VariableReferenceExpression expression, StaticContext argument) {
SequenceType variableType = expression.getType();
SequenceType variableType = expression.getActualType();
if(variableType == null){
System.out.println("variable reference type was null so");
variableType = SequenceType.MOST_GENERAL_SEQUENCE_TYPE;
// if is null, no 'as [SequenceType]' part was present in the declaration, therefore we infer it
System.out.println("variable reference type was null so we infer it");
variableType = argument.getVariableSequenceType(expression.getVariableName());
// we also set variableReference type
expression.setType(variableType);
}
System.out.println("visiting variable reference with type: " + variableType);
expression.setInferredSequenceType(variableType);
Expand Down Expand Up @@ -596,5 +600,26 @@ public StaticContext visitComparisonExpr(ComparisonExpression expression, Static



// endregion

// region module

@Override
public StaticContext visitVariableDeclaration(VariableDeclaration expression, StaticContext argument) {
// if expression has no type we infer it, and overwrite the type in the correspondent InScopeVariable
visitDescendants(expression, argument);
if(expression.getActualSequenceType() == null){
SequenceType inferredType = expression.getExpression().getInferredSequenceType();
if(inferredType == null){
throw new OurBadException("The child expression of VariableDeclaration has no inferred type");
}
// TODO: should I also change the variableDeclaration SequenceType?
argument.replaceVariableSequenceType(expression.getVariableName(), inferredType);
}

return argument;
}


// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ public StaticContext visitVariableReference(VariableReferenceExpression expressi
expression.getMetadata()
);
} else {
// note: sequence type can be null
expression.setType(argument.getVariableSequenceType(variableName));
ExecutionMode mode = argument.getVariableStorageMode(variableName);
if (this.visitorConfig.setUnsetToLocal() && mode.equals(ExecutionMode.UNSET)) {
mode = ExecutionMode.LOCAL;
}
expression.setHighestExecutionMode(mode);
// TODO: check staticContext available
return argument;
}
}
Expand Down Expand Up @@ -414,7 +416,7 @@ public StaticContext visitVariableDeclaration(VariableDeclaration variableDeclar
// first pass.
argument.addVariable(
variableDeclaration.getVariableName(),
variableDeclaration.getSequenceType(),
variableDeclaration.getActualSequenceType(),
variableDeclaration.getMetadata(),
variableDeclaration.getVariableHighestStorageMode(this.visitorConfig)
);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/rumbledb/compiler/TranslationVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1398,19 +1398,19 @@ private ExceptionMetadata createMetadataFromContext(ParserRuleContext ctx) {

@Override
public Node visitVarDecl(JsoniqParser.VarDeclContext ctx) {
// if there is no 'as sequenceType' is set to null to differentiate from the case of 'as item*'
// but it is actually treated as if it was item*
SequenceType seq = null;
boolean external;
Name var = ((VariableReferenceExpression) this.visitVarRef(ctx.varRef())).getVariableName();
if (ctx.sequenceType() != null) {
seq = this.processSequenceType(ctx.sequenceType());
} else {
seq = SequenceType.MOST_GENERAL_SEQUENCE_TYPE;
}
external = (ctx.external != null);
Expression expr = null;
if (ctx.exprSingle() != null) {
expr = (Expression) this.visitExprSingle(ctx.exprSingle());
if (!seq.equals(SequenceType.MOST_GENERAL_SEQUENCE_TYPE)) {
if (seq != null) {
expr = new TreatExpression(expr, seq, ErrorCode.UnexpectedTypeErrorCode, expr.getMetadata());
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/rumbledb/context/StaticContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ private InScopeVariable getInScopeVariable(Name varName) {
}
}

// replace the sequence type of an existing InScopeVariable, throws an error if the variable does not exists
public void replaceVariableSequenceType(Name varName, SequenceType newSequenceType){
InScopeVariable variable = getInScopeVariable(varName);
this.inScopeVariables.replace(varName, new InScopeVariable(varName, newSequenceType, variable.getMetadata(), variable.getStorageMode()));
}

public SequenceType getVariableSequenceType(Name varName) {
return getInScopeVariable(varName).getSequenceType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ public boolean external() {
return this.external;
}

// return item* if sequenceType is [null]
public SequenceType getSequenceType() {
return this.sequenceType == null ? SequenceType.MOST_GENERAL_SEQUENCE_TYPE : this.sequenceType;
}

// as above but does NOT default to item*
public SequenceType getActualSequenceType() {
return this.sequenceType;
}

Expand Down Expand Up @@ -115,7 +121,7 @@ public void print(StringBuffer buffer, int indent) {
+ (this.variableName)
+ ", "
+ (this.external ? "external, " : "")
+ this.sequenceType.toString()
+ this.getSequenceType().toString()
+ ") "
);
buffer.append(" | " + this.highestExecutionMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public Name getVariableName() {
return this.name;
}

// default to item* if type is null
public SequenceType getType() {
return this.type == null ? SequenceType.MOST_GENERAL_SEQUENCE_TYPE : this.type;
}

public SequenceType getActualType() {
return this.type;
}

Expand Down

0 comments on commit 099221f

Please sign in to comment.