Skip to content

Commit

Permalink
Make sure lexicalFrame is never null
Browse files Browse the repository at this point in the history
This simplifies the code a bit.

#5637

RELNOTES: None.
PiperOrigin-RevId: 210410389
  • Loading branch information
laurentlb authored and Copybara-Service committed Aug 27, 2018
1 parent 6d438fd commit 5d26ef1
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions src/main/java/com/google/devtools/build/lib/syntax/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private static final class Continuation {
@Nullable final Continuation continuation;

/** The lexical Frame of the caller. */
final LexicalFrame lexicalFrame;
final Frame lexicalFrame;

/** The global Frame of the caller. */
final GlobalFrame globalFrame;
Expand All @@ -482,7 +482,7 @@ private static final class Continuation {
@Nullable Continuation continuation,
BaseFunction function,
@Nullable FuncallExpression caller,
LexicalFrame lexicalFrame,
Frame lexicalFrame,
GlobalFrame globalFrame,
@Nullable LinkedHashSet<String> knownGlobalVariables) {
this.continuation = continuation;
Expand Down Expand Up @@ -655,7 +655,7 @@ public int hashCode() {
* Static Frame for lexical variables that are always looked up in the current Environment
* or for the definition Environment of the function currently being evaluated.
*/
private LexicalFrame lexicalFrame;
private Frame lexicalFrame;

/**
* Static Frame for global variables; either the current lexical Frame if evaluation is currently
Expand Down Expand Up @@ -728,7 +728,7 @@ public int hashCode() {
*/
void enterScope(
BaseFunction function,
LexicalFrame lexical,
Frame lexical,
@Nullable FuncallExpression caller,
GlobalFrame globals) {
continuation =
Expand Down Expand Up @@ -782,7 +782,7 @@ public void checkLoadingPhase(String symbol, Location loc) throws EvalException
* as opposed to inside the body of a function.
*/
boolean isGlobal() {
return lexicalFrame == null;
return lexicalFrame instanceof GlobalFrame;
}

@Override
Expand All @@ -791,11 +791,6 @@ public Mutability mutability() {
return dynamicFrame.mutability();
}

/** Returns the current Frame, in which variable side-effects happen. */
private Frame currentFrame() {
return isGlobal() ? globalFrame : lexicalFrame;
}

/** Returns the global variables for the Environment (not including dynamic bindings). */
public GlobalFrame getGlobals() {
return globalFrame;
Expand Down Expand Up @@ -863,6 +858,7 @@ private Environment(
@Nullable String fileContentHashCode,
Phase phase,
@Nullable Label callerLabel) {
this.lexicalFrame = Preconditions.checkNotNull(globalFrame);
this.globalFrame = Preconditions.checkNotNull(globalFrame);
this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame);
Preconditions.checkArgument(!globalFrame.mutability().isFrozen());
Expand Down Expand Up @@ -990,21 +986,11 @@ public Label getCallerLabel() {
* @return this Environment, in fluid style
*/
public Environment setupDynamic(String varname, Object value) {
if (dynamicFrame.get(varname) != null) {
if (lookup(varname) != null) {
throw new AssertionError(
String.format("Trying to bind dynamic variable '%s' but it is already bound",
varname));
}
if (lexicalFrame != null && lexicalFrame.get(varname) != null) {
throw new AssertionError(
String.format("Trying to bind dynamic variable '%s' but it is already bound lexically",
varname));
}
if (globalFrame.get(varname) != null) {
throw new AssertionError(
String.format("Trying to bind dynamic variable '%s' but it is already bound globally",
varname));
}
try {
dynamicFrame.put(this, varname, value);
} catch (MutabilityException e) {
Expand All @@ -1021,7 +1007,7 @@ public Environment setupDynamic(String varname, Object value) {
/** Remove variable from local bindings. */
void removeLocalBinding(String varname) {
try {
currentFrame().remove(this, varname);
lexicalFrame.remove(this, varname);
} catch (MutabilityException e) {
throw new AssertionError(e);
}
Expand All @@ -1048,7 +1034,7 @@ public Environment update(String varname, Object value) throws EvalException {
null, String.format("Trying to update read-only global variable '%s'", varname));
}
try {
currentFrame().put(this, varname, Preconditions.checkNotNull(value));
lexicalFrame.put(this, varname, Preconditions.checkNotNull(value));
} catch (MutabilityException e) {
// Note that since at this time we don't accept the global keyword, and don't have closures,
// end users should never be able to mutate a frozen Environment, and a MutabilityException
Expand Down Expand Up @@ -1096,11 +1082,9 @@ public Environment setupOverride(String varname, Object value) {
*/
public Object lookup(String varname) {
// Lexical frame takes precedence, then globals, then dynamics.
if (lexicalFrame != null) {
Object lexicalValue = lexicalFrame.get(varname);
if (lexicalValue != null) {
return lexicalValue;
}
Object lexicalValue = lexicalFrame.get(varname);
if (lexicalValue != null) {
return lexicalValue;
}
Object globalValue = globalFrame.get(varname);
Object dynamicValue = dynamicFrame.get(varname);
Expand Down Expand Up @@ -1138,9 +1122,8 @@ public void handleEvent(Event event) {
*/
public Set<String> getVariableNames() {
LinkedHashSet<String> vars = new LinkedHashSet<>();
if (lexicalFrame != null) {
vars.addAll(lexicalFrame.getTransitiveBindings().keySet());
}
vars.addAll(lexicalFrame.getTransitiveBindings().keySet());
// No-op when globalFrame = lexicalFrame
vars.addAll(globalFrame.getTransitiveBindings().keySet());
vars.addAll(dynamicFrame.getTransitiveBindings().keySet());
return vars;
Expand Down Expand Up @@ -1190,7 +1173,7 @@ public ImmutableList<DebugFrame> listFrames(Location currentLocation) {
ImmutableList.Builder<DebugFrame> frameListBuilder = ImmutableList.builder();

Continuation currentContinuation = continuation;
Frame currentFrame = currentFrame();
Frame currentFrame = lexicalFrame;

// if there's a continuation then the current frame is a lexical frame
while (currentContinuation != null) {
Expand Down

0 comments on commit 5d26ef1

Please sign in to comment.