Skip to content

Commit

Permalink
Got rid of the inBackend state handling in several visitors by usin…
Browse files Browse the repository at this point in the history
…g the new `getScopedBackend()` (#1392)
  • Loading branch information
quintesse committed Jul 30, 2015
1 parent 36299c4 commit 72f718e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public class ExpressionVisitor extends Visitor {
// private boolean isCondition;
private boolean dynamic;
private boolean inExtendsClause = false;
private Backend inBackend = null;
private TypeDeclaration constructorClass;

private Node ifStatementOrExpression;
Expand Down Expand Up @@ -196,19 +195,11 @@ public ExpressionVisitor(BackendSupport backendSupport) {
public ExpressionVisitor(TypecheckerUnit unit, BackendSupport backendSupport) {
this.unit = unit;
this.backendSupport = backendSupport;
String nat = unit.getPackage().getModule().getNativeBackend();
inBackend = Backend.fromAnnotation(nat);
}

@Override public void visit(Tree.CompilationUnit that) {
unit = that.getUnit();
Backend ib = inBackend;
String nat = unit.getPackage().getModule().getNativeBackend();
if (nat != null) {
inBackend = Backend.fromAnnotation(nat);
}
super.visit(that);
inBackend = ib;
}

private Declaration beginReturnDeclaration(Declaration d) {
Expand Down Expand Up @@ -1110,7 +1101,7 @@ else if (et!=null && unit.isEmptyType(et)) {
if (type!=null) {
Type t = type.getTypeModel();
if (type instanceof Tree.LocalModifier &&
!isNativeForWrongBackend()) {
!isNativeForWrongBackend(dec.getScopedBackend())) {
if (dec.isParameter()) {
type.addError("parameter may not have inferred type: '" +
dec.getName() +
Expand Down Expand Up @@ -1995,13 +1986,7 @@ else if (!hasError(se)) {
}

@Override public void visit(Tree.Declaration that) {
Backend ib = inBackend;
String nat = that.getDeclarationModel().getNativeBackend();
if (nat != null) {
inBackend = Backend.fromAnnotation(nat);
}
super.visit(that);
inBackend = ib;
}

private static VoidModifier fakeVoid(Node that) {
Expand Down Expand Up @@ -6038,7 +6023,7 @@ private TypedDeclaration resolveBaseMemberExpression(
that.getUnit());
if (member==null) {
if (!dynamic &&
!isNativeForWrongBackend() &&
!isNativeForWrongBackend(that.getScope().getScopedBackend()) &&
error) {
that.addError("function or value does not exist: '" +
name + "'", 100);
Expand Down Expand Up @@ -6357,7 +6342,7 @@ private void visitQualifiedMemberExpression(
ptr.getFullType(wrap(ptr.getType(),
receivingType, that)));
if (!dynamic &&
!isNativeForWrongBackend() &&
!isNativeForWrongBackend(that.getScope().getScopedBackend()) &&
!isAbstraction(member) &&
isTypeUnknown(fullType)) {
//this occurs with an ambiguous reference
Expand Down Expand Up @@ -6550,7 +6535,7 @@ private void visitBaseMemberExpression(
tal, outerType, typeArgs,
pr.getFullType());
if (!dynamic &&
!isNativeForWrongBackend() &&
!isNativeForWrongBackend(that.getScope().getScopedBackend()) &&
!isAbstraction(member) &&
isTypeUnknown(fullType)) {
that.addError("could not determine type of function or value reference: '" +
Expand Down Expand Up @@ -6630,7 +6615,9 @@ private TypeDeclaration resolveBaseTypeExpression(
that.getEllipsis(),
that.getUnit());
if (type==null) {
if (!dynamic && !isNativeForWrongBackend() && error) {
if (error &&
!dynamic &&
!isNativeForWrongBackend(that.getScope().getScopedBackend())) {
that.addError("type does not exist: '" +
name + "'",
102);
Expand Down Expand Up @@ -9045,11 +9032,13 @@ private Declaration handleAbstractionOrHeader(Declaration dec,
Declaration hdr = null;
Module ctxModule = that.getUnit().getPackage().getModule();
Module decModule = dec.getUnit().getPackage().getModule();
String inBackend = that.getScope().getScopedBackend();
if (dec.isNative()) {
Backend be = Backend.fromAnnotation(inBackend);
BackendSupport backend =
inBackend == null ?
be == null ?
backendSupport :
inBackend.backendSupport;
be.backendSupport;
if (dec.isNativeHeader()) {
hdr = dec;
impl = getNativeDeclaration(hdr, backend);
Expand Down Expand Up @@ -9077,9 +9066,10 @@ private Declaration handleAbstractionOrHeader(Declaration dec,
&& !isInNativeContainer((Declaration)that.getScope()))
&& (inBackend == null
|| impl.isNative()
&& !impl.getNativeBackend().equals(inBackend.nativeAnnotation)
&& !impl.getNativeBackend().equals(inBackend)
|| decModule.isNative()
&& !decModule.getNativeBackend().equals(inBackend.nativeAnnotation))) {
&& !decModule.getNativeBackend().equals(inBackend))) {
String nb = that.getScope().getScopedBackend();
if (inBackend != null) {
that.addError("native declaration: '" +
((Declaration)that.getScope()).getName(unit) +
Expand Down Expand Up @@ -9133,8 +9123,10 @@ private Declaration handleAbstractionOrHeader(Declaration dec,
// We use this to check for similar situations as "dynamic"
// where in this case the backend compiler can't check the
// validity of the code for the other backend
private boolean isNativeForWrongBackend() {
return inBackend != null &&
!backendSupport.supportsBackend(inBackend);
private boolean isNativeForWrongBackend(String backend) {
Backend be;
return backend != null &&
(be = Backend.fromAnnotation(backend)) != null &&
!backendSupport.supportsBackend(be);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;
import java.util.Set;

import com.redhat.ceylon.common.Backend;
import com.redhat.ceylon.common.BackendSupport;
import com.redhat.ceylon.compiler.typechecker.context.TypecheckerUnit;
import com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer;
Expand Down Expand Up @@ -47,8 +46,6 @@ public class ImportVisitor extends Visitor {
private TypecheckerUnit unit;
private final BackendSupport backendSupport;

private Backend inBackend = null;

public ImportVisitor(BackendSupport backendSupport) {
this.backendSupport = backendSupport;
}
Expand All @@ -57,23 +54,11 @@ public ImportVisitor(TypecheckerUnit unit,
BackendSupport backendSupport) {
this.unit = unit;
this.backendSupport = backendSupport;
String nat =
unit.getPackage()
.getModule()
.getNativeBackend();
inBackend = Backend.fromAnnotation(nat);
}

@Override public void visit(Tree.CompilationUnit that) {
unit = that.getUnit();
Backend ib = inBackend;
String nat =
unit.getPackage()
.getModule()
.getNativeBackend();
inBackend = Backend.fromAnnotation(nat);
super.visit(that);
inBackend = ib;
HashSet<String> set = new HashSet<String>();
for (Tree.Import im: that.getImportList().getImports()) {
Tree.ImportPath ip = im.getImportPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public class TypeVisitor extends Visitor {
private boolean inDelegatedConstructor;
private boolean inTypeLiteral;
private boolean inExtendsOrClassAlias;
private Backend inBackend = null;

public TypeVisitor(BackendSupport backendSupport) {
this.backendSupport = backendSupport;
Expand All @@ -87,33 +86,15 @@ public TypeVisitor(TypecheckerUnit unit,
BackendSupport backendSupport) {
this.unit = unit;
this.backendSupport = backendSupport;
String nat =
unit.getPackage()
.getModule()
.getNativeBackend();
inBackend = Backend.fromAnnotation(nat);
}

@Override public void visit(Tree.CompilationUnit that) {
unit = that.getUnit();
Backend ib = inBackend;
String nat =
unit.getPackage()
.getModule()
.getNativeBackend();
inBackend = Backend.fromAnnotation(nat);
super.visit(that);
inBackend = ib;
}

@Override public void visit(Tree.Declaration that) {
Backend ib = inBackend;
String nat = that.getDeclarationModel().getNativeBackend();
if (nat != null) {
inBackend = Backend.fromAnnotation(nat);
}
super.visit(that);
inBackend = ib;
}

public void visit(Tree.GroupedType that) {
Expand Down Expand Up @@ -396,7 +377,7 @@ public void visit(Tree.BaseType that) {
null, false, unit);
}
if (type==null) {
if (!isNativeForWrongBackend()) {
if (!isNativeForWrongBackend(scope.getScopedBackend())) {
that.addError("type declaration does not exist: '" +
name + "'", 102);
unit.getUnresolvedReferences().add(id);
Expand Down Expand Up @@ -482,7 +463,7 @@ public void visit(Tree.QualifiedType that) {
getTypeMember(d, name,
null, false, unit);
if (type==null) {
if (!isNativeForWrongBackend()) {
if (!isNativeForWrongBackend(that.getScope().getScopedBackend())) {
if (d.isMemberAmbiguous(name, unit, null, false)) {
that.addError("member type declaration is ambiguous: '" +
name + "' for type '" +
Expand Down Expand Up @@ -1628,10 +1609,16 @@ private Declaration handleHeader(Declaration dec,
Node that) {
if (Backend.None.nativeAnnotation.equals(dec.getNativeBackend())
&& !backendSupport.supportsBackend(Backend.None)) {
BackendSupport backend =
inBackend == null ?
backendSupport :
inBackend.backendSupport;
Scope scope = that.getScope();
if (scope == dec) {
scope = scope.getScope();
}
String inBackend = scope.getScopedBackend();
Backend be = Backend.fromAnnotation(inBackend);
BackendSupport backend =
be == null ?
backendSupport :
be.backendSupport;
Declaration hdr = dec;
if (!hdr.isNativeHeader()) {
hdr = getNativeHeader(dec);
Expand Down Expand Up @@ -1660,9 +1647,10 @@ else if (hdr==null) {

// We use this for situations where the backend compiler can't check the
// validity of the code for the other backend
private boolean isNativeForWrongBackend() {
return inBackend != null &&
!backendSupport.supportsBackend(inBackend);
private boolean isNativeForWrongBackend(String backend) {
Backend be;
return backend != null &&
(be = Backend.fromAnnotation(backend)) != null &&
!backendSupport.supportsBackend(be);
}

}

0 comments on commit 72f718e

Please sign in to comment.